JavaScript from Null: Cross-Browser Event Binding
Tutorial Details
- Subject: JavaScript
- Estimated Difficulty: Beginner - Intermediate
- Tutorial Format: 29 Minute Screencast
Download Source Files
The Full Series
In chapter five of this series, we jumped into the muddy world of event listeners. In that episode, we only got our feet wet; however, today, we’ll take things a step further as we implement a far more efficient solution. Along the way, we’ll learn a plethora of new techniques.
As with every JavaScript from Null screencast, it’s not essential that you view the previous entries in the series before watching.
Chapter 6: Cross-Browser Event Binding
Premium Members: Download this Video ( Must be logged in)
Our Final Code
var addEvent = (function( window, document ) {
if ( document.addEventListener ) {
return function( elem, type, cb ) {
if ( (elem && !elem.length) || elem === window ) {
elem.addEventListener(type, cb, false );
}
else if ( elem && elem.length ) {
var len = elem.length;
for ( var i = 0; i < len; i++ ) {
addEvent( elem[i], type, cb );
}
}
};
}
else if ( document.attachEvent ) {
return function ( elem, type, cb ) {
if ( (elem && !elem.length) || elem === window ) {
elem.attachEvent( 'on' + type, function() { return cb.call(elem, window.event) } );
}
else if ( elem.length ) {
var len = elem.length;
for ( var i = 0; i < len; i++ ) {
addEvent( elem[i], type, cb );
}
}
};
}
})( this, document );
// Example Usage
var lis = document.getElementsByTagName('li');
addEvent( window, 'click', function() {
this.style.border = '1px solid red';
});
Please note that this code is slightly revised, based upon some excellent feedback in the comments section.
Tags: Videos
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
- http://laroouse.com esranull
- http://www.facebook.com/Dev.MZahran Mohamed Zahran
- Etrimon
- Adam
- http:/pablolarah.cl/ Pablo Lara H
- Adam
- Max
- kawohi
- Eduardo Barros
- herpderp
- http://net.tutsplus.com Jeffrey Way
- Hassan
- http://net.tutsplus.com Jeffrey Way
- Yosy
- RockStar
- http://www.jenileigh.com Jen Kelly
- RockStar
- Kurt
- http://shay.co/ Shay Ben Moshe
- http://www.facebook.com/Dev.MZahran Mohamed Zahran
- http://www.jsxtech.com Jaspal Singh
- http://Byte-News.com Ahmed CZ
- http://Byte-News.com Ahmed CZ
- http://Byte-News.com Ahmed CZ
- http://Byte-News.com Ahmed CZ
- Andreas Madsen
- http://net.tutsplus.com Jeffrey Way
- Andreas Madsen
- http://www.web30textures.com/ GKL
- http://thedevelopertuts.com thedevelopertuts
- http://www.designmango.com DesignMango
- http://www.websolpk.com imran khan
- http://www.silverjewelryworld.com Kam
- http://www.tutoriallounge.com Tutorial Lounge
- http://twitter.com/rem Remy Sharp
- James
- Gabriel Cunha
- http://www.isaacvanname.com Isaac Van Name
- http://www.ultramegatech.com/ Steve Guidetti
- http://xandercs.com Xander
- Rob Henry
- Rob Henry
- http://holamiamor.tistory.com Irene
- Dionis
- Alex
- chichibek
- http://www.webcome.ro Andrei Dorin
- Andre Dublin
- Danielson
- Michael
- Michael
- James Fair
- Yoav
- Ivan
- Alysson Bortoli
- Arturo
- http://twitter.com/_jamonn Marco
- jackie
- Josh
- Josh
- Zlatev
- Andy
- Andy
