简体   繁体   中英

How to prototype extend the document object in IE9?

I want to override the native document.write function so I'm doing this:

HTMLDocument.prototype.write = function(arg) {
    alert('Do Something!');
};

This works in every browser except IE9 where it throws the following error:

'HTMLDocument' is undefined

It even works in IE 8. How to extend the document object in IE9?

Apparently, somewhere before IE 9 was about to be published HTMLDocument was replaced with Document and it was like this in IE 10 as well. However, in IE 11 both objects are back and following returns true :

Document.prototype.write === HTMLDocument.prototype.write

even though Document and HTMLDocument reference different objects. So to do what you want for IE 9 and IE 10 (I didn't check IE 8 behavior) you could do something like:

Document.prototype.write = function(arg) {
    alert('Do Something!');
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM