Search code examples
javascriptdomprototypeextendchain

"Object doesn't support method" error when extending Element.prototype in IE8


I understand that IE8 supports extension of the Element object. I tried a simple example, and it works in Google Chrome but not in IE8. Here is my code, and here is a jsfiddle to see my code:

HTML

<div id='test'>Hi</div>

JavaScript

Element.prototype.test = function(){
  alert('yup');
}

document.getElementById('test').test();

Errors

//jsfiddle.net => Object doesn't support property or method 'test'
//local test => 'Element' is undefined

What am I doing wrong?


UPDATE

Ok as for the local error, it seems that IE8 didn't like my <!DOCTYPE> tag. So I found another one. =p IE8 is very strict on things like that. If an element isn't valid HTML, IE8 won't pick it up in things such as document.getElementsByTagName().


Solution

  • MooTools overrides the baseline Element.

    Check out

    var e = document.getElementById('test');
    console.log(e instanceof Element);
    

    with MooTools on it returns false, with anything but MooTools it should return true.