Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
The minimal event normalizer
JavaScript
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
test
2k.js
LICENSE
README

README

2k.js – A minimal JavaScript event normalizer

The purpose of 2k.js is to normalize event handling in common
browsers to support the W3C DOM Level 3 Events Specification.

2k.js creates the global namespace 'E' that acts as a wrapper
for all event handling. It also provides a simplified syntax
for binding, triggering, unbinding and other helper methods.

Core features:

* W3C DOM Level 3 compliant: http://www.w3.org/TR/DOM-Level-3-Events/
* Very lightweight (~1.5k gzipped)
* No dependencies
* Allows capturing and bubbling events (jQuery only allows bubbling)
* Allows a custom scope (the 'this' context) inside the callback
* Normalizes mousenter and mouseleave for simple hovers
* Allows simple event delegation
* Simple syntax
* Test suite includes IE7+, opera, safari, chrome, firefox

Usage:
    E.bind( HTMLElement, type, callback[, capture][, scope] );
    E.one( HTMLElement, type, callback[, capture][, scope] );
    E.unbind( [HTMLElement][, type][, callback][, capture] );
    E.delegate( [HTMLElement][, type][, callback] );
    E.trigger( HTMLElement, type[, props] );

You can also use shorthand for most events, jQuery style:
    E.click( HTMLElement, callback );
    E.resize( window, callback );
    E.click( HTMLElement ); // triggers a click event on HTMLElement

Another way to use 2k is to use it as a function with the element as argument, then chain methods on top of it:
    E( HTMLElement ).click( callback ).mousenter( callback );

You can bind and unbind multiple event types by separating them with a space:
    E.bind(document, 'mousedown mouseup', function(e) {
        console.log(e.type);
    });

Normalizes:
    event.target,
    event.currentTarget
    event.preventDefault
    event.relatedTarget
    event.stopPropagation
    event.stopImmediatePropagation
    event.which
    event.pageX
    event.pageY
    event.bubbles
    event.defaultPrevented
    event.cancelBubble
    event.isTrusted
    event.eventPhase
    event.cancelable
    event.timeStamp

Also normalizes event capturing for IE (!)

Note that DOMEvents like DOMAttrModified are not normalized, neither is composition events.
See full reference here: http://www.w3.org/TR/DOM-Level-3-Events/#table-event-summary

Tested in IE7+, FF, Chrome, Safari, Opera
Linted.

minified size = 3k
gZipped minified size = 1.5k (!!)

Copyright (c) 2011, Aino
Licensed under the MIT license.
Something went wrong with that request. Please try again.