I want to add a method to all HTML elements and the document object in JavaScript.
After some googling I found http://krook.org/jsdom/.
I concluded that adding a method to the Node class would do the job,
and indeed in Firefox and Chrome this worked.
Below is pretty much what I did.
<script>
Node.prototype.foo=function(selector){
alert('succes');
}
document.foo();
document.getElementById("foo").foo();
</script>
In Internet Explorer it causes an error because Node is not defined.
Does anyone know how to do this in Internet Explorer?
Oh also, I’m using Internet Explorer 9.
Thank you.
The IE DOM won’t allow JavaScript to access the constructors for prototyping, so element/Node prototyping is not supported by the IE DOM natively (IE 10 runs your code fine, as I suspect does IE 0).
What are you trying to achieve? There are arguments against doing DOM extensions – there is probably another way to do it.