Issue
Are are three "proper" ways to listen for events. In this example, we are listening for the load
event:
1. <body onload="doSomething();">
2. document.body.onload = doSomething;
3. document.body.addEventListener('load', doSomething);
Can all JavaScript events be listened to in all three of these ways?
Or are there events which are only dispatched (the third line), and cannot be "listened" to as an event attribute (the first line) or as a property of the element (the second line)? Or vice versa, are never dispatched, but only have settable "callback" properties?
Solution
Can all JavaScript events be listened to in all three of these ways?
No.
Some events in JavaScript are triggered on objects that are not part of the DOM, so there is no way to add an inline attribute in HTML when there is no HTML.
For example, MessageEvent
s for WebSockets, and ProgressEvent
s for XmlHttpRequests.
There is a reference list of possible on(eventname)
attributes in the HTML5 spec:
- onabort
- onblur*
- oncancel
- oncanplay
- oncanplaythrough
- onchange
- onclick
- onclose
- oncontextmenu
- oncuechange
- ondblclick
- ondrag
- ondragend
- ondragenter
- ondragexit
- ondragleave
- ondragover
- ondragstart
- ondrop
- ondurationchange
- onemptied
- onended
- onerror*
- onfocus*
- oninput
- oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload*
- onloadeddata
- onloadedmetadata
- onloadstart
- onmousedown
- onmouseenter
- onmouseleave
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onmousewheel
- onpause
- onplay
- onplaying
- onprogress
- onratechange
- onreset
- onresize*
- onscroll*
- onseeked
- onseeking
- onselect
- onshow
- onsort
- onstalled
- onsubmit
- onsuspend
- ontimeupdate
- ontoggle
- onvolumechange
- onwaiting
The attributes marked with an asterisk have a different meaning when specified on body elements as those elements expose event handlers of the Window object with the same names.
Regarding how events are handled on non-DOM objects, each feature has its own specification and API. Each feature is therefore subject to change along with the standard inconsistencies between browsers.
Answered By - zzzzBov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.