Event Listener Cross Browser
Login or Register to Bookmark this snippet
Add an Event to an element that works Cross Browser
@elm: element, can be the element onject or string ID of the element
@evtype: string event name without "on" prefix.
@fn: function reference, executed with event
@usecapture: true of false. Use false if you're unsure what this does.
function addEvent (elm, evtype, fn, usecapture) {
//check if all required variables are defined
if (elm && evtype && fn) {
//all variables required have been supplied
if (typeof elm == "string") {
//the id was supplied, get the object reference
elm = xGetElementById(elm);
}
//add listener
if (elm.addEventListener) {
elm.addEventListener(evtype, fn, usecapture);
return true;
} else if (elm.attachEvent) {
var r = elm.attachEvent("on" + evtype, fn);
return r;
} else {
elm["on" + evtype] = fn;
}
} else {
//notify user of problem
alert('Incorrect number of parameters for addEvent function');
return false;
}
}
Added by JC on 12th November, 2007
There are no comments about this snippet.
You must be registered and logged in to post a comment.
Login here to post a comment