Skip to content

Instantly share code, notes, and snippets.

@TheJosh
Forked from jonathantneal/README.md
Last active December 24, 2015 08:39
Show Gist options
  • Save TheJosh/6771612 to your computer and use it in GitHub Desktop.
Save TheJosh/6771612 to your computer and use it in GitHub Desktop.
Updated polyfill to normalize "which" for mouse events.

EventListener Polyfill

Is IE8 your new IE6? Level the playing field with polyfills.

This script polyfills addEventListener, removeEventListener, and dispatchEvent. It is less than half a kilobyte minified and gzipped.

addEventListener

addEventListener registers a single event listener on a single target.

Syntax

target.addEventListener(type, listener);

type: A string representing the event type to listen for.

listener: The object that receives a notification when an event of the specified type occurs. This must be an object implementing the EventListener interface, or simply a JavaScript function.

It should be noted that useCapture has not been polyfilled.

removeEventListener

removeEventListener unregisters a single event listener on a single target.

Syntax

target.removeEventListener(type, listener);

type: A string representing the event type being removed.

listener: The EventListener object or function to be removed.

It should be noted that useCapture has not been polyfilled.

dispatchEvent

Dispatches an event into the event system. The event is subject to the same capturing and bubbling behavior as directly dispatched events.

Syntax

bool = target.dispatchEvent(event);

event: An event object to be dispatched.

It should be noted that document.createEvent has not been polyfilled.

!window.addEventListener && (function (WindowPrototype, DocumentPrototype, ElementPrototype, addEventListener, removeEventListener, dispatchEvent, registry) {
WindowPrototype[addEventListener] = DocumentPrototype[addEventListener] = ElementPrototype[addEventListener] = function (type, listener) {
var target = this;
registry.unshift([target, type, listener, function (event) {
event.currentTarget = target;
event.preventDefault = function () { event.returnValue = false };
event.stopPropagation = function () { event.cancelBubble = true };
event.target = event.srcElement || target;
if (!event.which && event.button !== undefined) {
event.which = ( event.button & 1 ? 1 : ( event.button & 2 ? 3 : ( event.button & 4 ? 2 : 0 ) ) );
}
listener.call(target, event);
}]);
this.attachEvent("on" + type, registry[0][3]);
};
WindowPrototype[removeEventListener] = DocumentPrototype[removeEventListener] = ElementPrototype[removeEventListener] = function (type, listener) {
for (var index = 0, register; register = registry[index]; ++index) {
if (register[0] == this && register[1] == type && register[2] == listener) {
return this.detachEvent("on" + type, registry.splice(index, 1)[0][3]);
}
}
};
WindowPrototype[dispatchEvent] = DocumentPrototype[dispatchEvent] = ElementPrototype[dispatchEvent] = function (eventObject) {
return this.fireEvent("on" + eventObject.type, eventObject);
};
})(Window.prototype, HTMLDocument.prototype, Element.prototype, "addEventListener", "removeEventListener", "dispatchEvent", []);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment