Skip to content

Instantly share code, notes, and snippets.

@alanedwardes
Created October 15, 2009 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanedwardes/211143 to your computer and use it in GitHub Desktop.
Save alanedwardes/211143 to your computer and use it in GitHub Desktop.
// By Alan Edwardes - http://alanedwardes.com/contact
// Licensed under The GNU General Public License (GPL)
// -------------------------------------------------------------
// A simple events wrapper function for adding events that works
// cross browser, and allows you to use the first parameter of
// the callback function as a reference to the object that the
// event was fired from, which replaces IE's lack of support
// for the "this" variable.
//
// Ex. Usage:
// ------------------------------------------------------------------------
// AddEvent(document.GetElementById('SomeElement'),'mousedown',MyCallback);
//
// function MyCallback(Element){
// alert('This is the content of my element: ' + Element.innerHTML);
// }
function AddEvent(Element,Event,Callback){
if(window.addEventListener){
Element.addEventListener(Event,function(){
Callback(Element);
},false);
}else if(window.attachEvent){
Element.attachEvent('on'+Event,function(){
Callback(Element);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment