What order of precedence are events handled in JavaScript?
Here are the events in alphabetical order...
- onabort - Loading of an image is interrupted
- onblur - An element loses focus
- onchange - The user changes the content of a field
- onclick - Mouse clicks an object
- ondblclick - Mouse double-clicks an object
- onerror - An error occurs when loading a document or an image
- onfocus - An element gets focus
- onkeydown - A keyboard key is pressed
- onkeypress - A keyboard key is pressed or held down
- onkeyup - A keyboard key is released
- onload - A page or an image is finished loading
- onmousedown - A mouse button is pressed
- onmousemove - The mouse is moved
- onmouseout - The mouse is moved off an element
- onmouseover - The mouse is moved over an element
- onmouseup - A mouse button is released
- onreset - The reset button is clicked
- onresize - A window or frame is resized
- onselect - Text is selected
- onsubmit - The submit button is clicked
- onunload - The user exits the page
What order are they handled out of the event queue?
The precedence is not first-in-first-out (FIFO) or so I believe.
This was not, so far as i know, explicitly defined in the past. Different browsers are free to implement event ordering however they see fit. While most are close enough for all practical purposes, there have been and continue to be some odd edge cases where browsers differ somewhat (and, of course, the many more cases where certain browsers fail to send certain events at all).
That said, the HTML 5 draft recommendation does make an attempt to specify how events will be queued and dispatched - the event loop:
Note that last bit: it is up to the browser implementation to determine which events will be grouped together and processed in order, as well as the priority given to any particular type of event. Therefore, there's little reason to expect all browsers to dispatch all events in a fixed order, now or in the future.
If you're looking at mouse/touch events, Patrick H. Lauke has published a talk on the subject. Definitely an interesting read – and deals with all the quirks of different browsers, different devices and different standards.
He also bundles a comprehensive set of tests.
For anyone wanting to know the sequence relative events get called in, see below. So far I have only tested in Chrome.