MSThis - simulate W3C events in IE
Overview
This script simulates W3C event handling in IE, fixing the old problem of IE registering events on the window object, not the element that triggered the event. This can cause real headaches, particularly in object-oriented Javascript.
Events are added using the W3C addEventListener() method. Note this script does not bring event capture support to IE, which remains unsupported in MS browsers.
Example
Click me and I'll turn red and bold, even in IE!
Here's the code for this example:
1$(function() {
2 registerEvent(document.getElementById('myLink'), 'click', function() {
3 this.style.color = '#f00';
4 this.style.fontWeight = 'bold';
5 });
6});
Usage
The script creates a global function, registerEvent, which accepts the same arguments as the standard addEventListener except the first argument should be the element to bind to.
Notes
This script works by storing an event handler on an element, then firing them as methods of it. It is cross-browser compatible, including IE6.
Comments (2)
OKoweb, at 21/09/'10 13:39, said:
OKoweb, at 22/09/'10 09:07, said: