2015-10-20 56 views
0

我要重写本地document.write功能,所以我这样做:如何在IE9中扩展文档对象的原型?

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

这个工程除了IE9浏览器的每一个地方,它抛出以下错误:

'HTMLDocument' is undefined

它甚至作品在IE 8中如何在IE9中扩展文档对象?

+1

你为什么不分配给'document.write'?你操纵很多不同的文件吗? – Touffy

+1

如果我记得正确,那么IE 9应该支持'HTMLDocument',所以这个消息表明IE是兼容模式而不是标准模式。 –

+0

您可以尝试使用元标记更改文档模式。' – stekhn

回答

0

显然,在IE 9即将发布之前的某个地方,HTMLDocument已被Document替代,IE 10中也是这样。然而,在IE 11的两个对象是背部和下返回

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

即使文档和参考HTMLDocument的不同对象。所以要做你想要的IE 9和IE 10(我没有检查IE 8的行为),你可以这样做:

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

最终做了'var document_extend = typeof HTMLDocument!=='undefined'? HTMLDocument:Document;'then'document_extend.prototype.write = function(arg)...' – TK123