Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - Firefox behavior blur by mouse click. Bug?

Scenario: display alert message onblur (or onchange) as a part of javascript field validation.

User action with onblur:
1) click inside input
2) click outside the input
3) close the alert message
4) move mouse around

Result: mousedown seems to be performed at the position where you clicked out before the alert came up -- elements on the page get selected when you move the mouse around.
Note: This doesn't happen when tabbing out of the input.

Demo: http://jsfiddle.net/s9sc4/

<body>
Click inside the input and then outside of it.
<input type="text" onblur="alert('Close this alert message and move the mouse around.');" />
TEST TEST TEST
</body>

Reproduced on:
Firefox 28 and 29 Platforms: Windows 7 & 8 and OSX Mavericks (4 different machines).
Using a clean Firefox profile made no difference.

QUESTION: Is this a bug, or default behavior? Chrome, Safari and IE don't behave like this.
If it's as designed, do I need to do something with preventDefault or cancel bubbling/stop propagation after the alert to stop this behavior?

like image 571
Benjamin Jung Avatar asked May 02 '14 16:05

Benjamin Jung


1 Answers

You can try to add:

window.getSelection().removeAllRanges();

This will solve your issue.

To answer your question: this seems to be a bug of FireFox and needs a workaround. What happens is FireFox messed up the priority of events where focus is set first, prior to onblur. Browsers who don't have the bug will not fire the focus event when onblur occurs.

DEMO

like image 193
Tim Vermaelen Avatar answered Sep 19 '22 03:09

Tim Vermaelen