I am trying to add a method to an Event prototype. To call / install preventDefault()or, in IE-speak returnValue = falseand -if it is desirable - stopPropagation()/ cancelBubble = true;. I thought the code below would be enough.
Event = Event || window.Event;
if(!(Event.prototype.stopEvent))
{
Event.prototype.stopEvent = function(propagate)
{
"use strict";
propagate = (propagate ? true : false);
if (this.preventDefault)
{
this.preventDefault();
if (propagate === false)
{
this.stopPropagation();
}
}
else
{
this.returnValue = false;
this.cancelBubble = !propagate;
}
return this;
};
}
Which seems to work, as you can see here . This fiddle shows OKin IE8, firefox and chrome. Although, when I add this to my script, IE8 breaks into the first line, saying "Event undefined". Leaving "use strict";does not matter.
Reluctantly, I also tried this:
if (typeof Event === 'undefined')
{
var Event = window.Event || window.event;
}
But to no avail:, Error: 'Event.prototype' is null or not an objectso I got another 1 line. The fact is that the whole prototype method is to copy a copy from my script, but what I don’t notice here? Any ideas / suggestions?
thank
PS: I like Pure JavaScript, so please do not offer jQuery, prototypejs, dojo, ... as a solution. I just got rid of jQuery. (I like jQuery, but not necessary)
Update
I'm afraid the situation has gone worse. I found this link on MSDN . The whole page is dedicated to DOM Element prototypes. It is fair to say that they are available and available in IE8 (to some extent). On this page, this code caught my attention:Event.prototype.stopPropagation = function ()
{
this.cancelBubble = true;
};
Event.prototype.preventDefault = function ()
{
this.returnValue = false;
};
~ 3/4- , "Powerful Scenarios". , , , , : jsfiddle, , jsfiddle ( ) IE8. , , , . :
Event.prototype.stopPropagation = function ()
{
if (this.stopPropagation)
{
return this.stopPropagation();
}
this.cancelBubble = true;
};
Event.prototype.preventDefault = function ()
{
if (this.preventDefault)
{
return this.preventDefault();
}
this.returnValue = false;
};