JavaScript Event Prototype in IE8

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;
//^^ makes the fiddle work on IE8 ^^
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;//FFS IE :-(
}

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;
};
+5
2

() , , . , Event IE (< 9), , (, : window.event). , , ( IE8 - 7):

(function()
{
        function ol(e)
        {//we have an event object
            e = e || window.event;
            if (!e.stopEvent)
            {
                if (Object && Object.getPrototypeOf)
                {//get the prototype
                    e = Object.getPrototypeOf(e);
                }
                else
                {//getting a prototype in IE8 is a bit of a faff, this expression works on most objects, though
                 //it part of my custom .getPrototypeOf method for IE
                    e = this[e.constructor.toString().match(/(function|object)\s+([A-Z][^\s(\]]+)/)[2]].prototype;
                }
                e.stopEvent = function(bubble)
                {//augment it (e references the prototype now
                    bubble = bubble || false;
                    if (this.preventDefault)
                    {
                        this.preventDefault();
                        if (!bubble)
                        {
                            this.stopPropagation();
                        }
                        return this;
                    }
                    this.returnValue = false;
                    this.cancelBubble = !bubble;
                    return this;
                };
            }
            alert(e.stopEvent ? 'ok' : 'nok');//tested, it alerts ok
            if (this.addEventListener)
            {
                this.removeEventListener('load',ol,false);
                return;
            }
            document.attachEvent('onkeypress',function(e)
            {
                e = e || window.event;
                if (e.stopEvent)
                {//another event, each time alerts ok
                    alert('OK!');
                }
            });
            this.detachEvent('onload',ol);
        }
        if (this.addEventListener)
        {
            this.addEventListener('load',ol,false);
        }
        else
        {
            this.attachEvent('onload',ol);
        }
})();

, doctype : <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">, FF, chrome IE 8, . <!DOCTYPE html> ,

, -...

+3

" ". JSFiddle DOCTYPE, , <!DOCTYPE html>, . , - DOCTYPE, Quirks. DOCTYPE , , .

+2

All Articles