Add an Event Cross-Browser
This is a method that checks for the standard addEventListener() or ActiveX's attachEvent() and will assign the event appropriately depending on the browser accessing the page.
This is a method that checks for the standard addEventListener() or ActiveX's attachEvent() and will assign the event appropriately depending on the browser accessing the page.
function addEvent(elem, event, func){
if(document.addEventListener){
elem.addEventListener(event, func, false);
}else if(document.attachEvent){
elem.attachEvent('on' + event, func);
}else{
elem['on' + event] = func;
}
}
Here is how it can be used.
// passing anonymous function
addEvent(window, 'load', function(){
// any code to run
});
addEvent(someForm, 'keyup', validateField);