Say I add events to an object using either addEventListener or attachEvent (depending on the browser); is it possible to later invoke those events programmatically?
The events handlers are added/removed using an object like this:
var Event = {
add: function(obj,type,fn) {
if (obj.attachEvent) {
obj.attachEvent('on'+type,fn);
} else {
obj.addEventListener(type,fn,false);
}
},
remove: function(obj,type,fn) {
if (obj.detachEvent) {
obj.detachEvent('on'+type,fn);
} else {
obj.removeEventListener(type,fn,false);
}
}
}
Or do I need to store copies of each handler and just add an Event.invoke(...) function?
Edit: Also, jQuery is not an option :D
If you enjoyed my content for some reason, I'd love to hear from you! Here are some options:
- You can buy me a coffee!
- Click here to post a comment!
- You can write a reply on your own site and submit the URL as a webmention via the form below.
- Or you can just contact me!
Can you not create functions that do the work required, run those from the events then run those same functions later when required?
via stackoverflow.comAs usual, you have to do it one way for Internet Explorer, and the correct way for everything else ;-)
For IE:
See the MSDN library for more info.
For the real browsers:
Note that, although the second, standards-based approach seems more long-winded, it is also considerably more flexible: check the documentation, starting with the Mozilla DOM Event Reference at https://developer.mozilla.org/en/DOM/event
Although only tangentially related to what you're trying to do (it's related to custom events, rather than normal ones) Dean Edwards has some example code at http://dean.edwards.name/weblog/2009/03/callbacks-vs-events/ that may be worth a look.
via stackoverflow.com