Cross-browsers events listener implementation
✓ Published0🌍 Public
Aabernier
Last edited Jul 6, 2011
Created on Jul 6, 2011
This example shows a cross-browser event listener abstraction that normalizes how JavaScript attaches and removes DOM event handlers. The code defines an `addListener` and `removeListener` pair, then initializes them by detecting the available API at runtime. It uses the standard `window.addEventListener` and `removeEventListener` methods when supported, falls back to Internet Explorer’s `document.attachEvent` and `detachEvent` with an `'on'` prefix, and finally uses direct `el['on' + type]` property assignment for older browsers. The logic is contained in a single self-invoking object that returns itself, ready for use without any external libraries.
AI-generated description