我们有一个 3rd 方 Web 应用程序,可以在 IE6 中运行,但不能在 IE8 中运行。
示例代码如下,在IE6中会弹出“message from .htc”消息,但在IE8中不会弹出。
测试.html
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>
<script type='text/javascript'>
//if comment the following line, or move this script in <body>,
//then HTC will work in IE8
document.write ("<h1>document.write() in <head></h1> some calendar codes");
</script>
</head>
<body style='behavior:url(test.htc)'>
HTML Components test
</body>
</html>
test.htc
<script type='text/javascript'>
alert ("message from .htc");
</script>
为什么会发生这样的事?有兼容的文档来解释这一点吗?
Solution
正如@Quentin 或另一位专家所说http://social.msdn.microsoft.com/Forums/en-US/iewebdevelopment/thread/c1f546f6-d7e1-4b46-a1c9-8f02eaf1286b http://social.msdn.microsoft.com/Forums/en-US/iewebdevelopment/thread/c1f546f6-d7e1-4b46-a1c9-8f02eaf1286b据说,IE8可能比IE6制定了严格的规则,IE8可能会将其视为损坏的HTML文档。
所以,我决定使用document.createElement http://msdn.microsoft.com/en-us/library/ms536389%28v=VS.85%29.aspx动态创建元素而不是document.write http://msdn.microsoft.com/en-us/library/ms536782%28v=VS.85%29.aspx,并将这些元素插入到 DOM 中延迟几秒后 http://msdn.microsoft.com/en-us/library/ms536753%28v=VS.85%29.aspx。经过一些测试,它终于在这个 test.html 和实际应用程序中都工作了。
测试-ie8-兼容.html
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>
<script type='text/javascript'>
function Delay_CreateCalendar()
{
var oContainer = document.createElement ("div");
var oCalendarIFrame = document.createElement ("iframe");
oContainer.appendChild (oCalendarIFrame);
document.body.insertBefore (oContainer);
}
setTimeout (Delay_CreateCalendar, 2000);
</script>
</head>
<body style='behavior:url(test.htc)'>
dhtml HTC 测试
</body>
</html>