Skip to content

Instantly share code, notes, and snippets.

@jrusbatch
Forked from jonathantneal/README.md
Last active August 29, 2015 14:01
Show Gist options
  • Save jrusbatch/17e6829c2b5694b986c1 to your computer and use it in GitHub Desktop.
Save jrusbatch/17e6829c2b5694b986c1 to your computer and use it in GitHub Desktop.

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.

(function(global) {
if (global.addEventListener) {
return;
}
(function (WindowPrototype, DocumentPrototype, ElementPrototype, addListener, removeListener, dispatchEvent, registry) {
WindowPrototype[addListener] = DocumentPrototype[addListener] = ElementPrototype[addListener] = function (type, listener) {
var target = this;
registry.unshift([target, type, listener, function (evt) {
evt.currentTarget = target;
evt.preventDefault = function () { evt.returnValue = false; };
evt.stopPropagation = function () { evt.cancelBubble = true; };
evt.target = evt.srcElement || target;
listener.call(target, evt);
}]);
this.attachEvent('on' + type, registry[0][3]);
};
WindowPrototype[removeListener] = DocumentPrototype[removeListener] = ElementPrototype[removeListener] = 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', []);
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment