gpt4 book ai didi

html - 如果在 中执行 document.write,为什么 DHTML 行为在 IE8 中不起作用?

转载 作者:搜寻专家 更新时间:2023-10-31 08:21:26 25 4
gpt4 key购买 nike

我们有一个在 IE6 中运行但在 IE8 中不运行的第 3 方 Web 应用程序。

示例代码如下,“message from .htc”消息在IE6中会弹出,在IE8中不会弹出。

test.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 &lt;head&gt;</h1> some calendar codes");
</script>
</head>

<body style='behavior:url(test.htc)'>
HTML Components test
</body>
</html>

测试.htc

<script type='text/javascript'>
alert ("message from .htc");
</script>

为什么会这样?任何兼容的文件来解释这一点?


解决方案

正如@Quentin 或来自 http://social.msdn.microsoft.com/Forums/en-US/iewebdevelopment/thread/c1f546f6-d7e1-4b46-a1c9-8f02eaf1286b 的另一位专家所说据说,IE8 可能制定了比 IE6 更严格的规则,IE8 可能会将其视为损坏的 HTML 文档。

所以,我决定使用 document.createElement动态创建元素而不是 document.write , 并将这些元素插入 DOM after some seconds delay .经过一些测试,它终于在这个 test.html 和实际应用中都起作用了。

test-ie8-compatible.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>

最佳答案

据推测,尽管有 namespace ,您还是将文档作为文本/html 提供。在 HTML 中,head 和 body 元素的开始和结束标记是可选的。 head 内不允许有 H1 元素。

因此,当您在结尾处记录 document.write 和 H1 时,您会触发头部的结尾和主体的开头。

我假设 IE 然后会忽略 body 元素的开始标记,因为它会创建第二个 body(这也是不允许的)。

关于html - 如果在 <head> 中执行 document.write,为什么 DHTML 行为在 IE8 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7593913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com