I’m trying to augment Object.prototype with an addEvent method that will add event listeners, and will work regardless of whether the browser is IE or not.
Here’s what I have: So far it seems to work in non-IE browsers, but in IE8 it doesn’t work. Where am I going wrong?
[CODE] (function(){
try{
if(document.getElementById){
if(window.attachEvent){
Object.prototype.addEvent = function(evt,func){
this.attachEvent( (“on”+evt), func );
}
}
else if(window.addEventListener){
Object.prototype.addEvent = function(evt,func){
this.addEventListener(evt,func,false);
}
}
else{
alert(“Old.”);
}
}
}
catch(e){
console.log(e);
}
})();
window.onload = function(){
var myDiv = document.getElementById(“redBox”);
myDiv.addEvent(“mouseover”, function(){alert(“Hello!”);} );
}
//Then there’s just an HTML div with an ID of “redBox”