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

发布于 2024-12-06 20:23:13 字数 2299 浏览 1 评论 0 原文

我们有一个 3rd 方 Web 应用程序,可以在 IE6 中运行,但不能在 IE8 中运行。

示例代码如下,在IE6中会弹出“message from .htc”消息,但在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>

test.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 延迟几秒后。经过一些测试,它终于在这个 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>

We have a 3rd party web application which works on in IE6, but not works in IE8.

The sample code is like below, the "message from .htc" message will popup in IE6, but not popup in 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 <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>

Why this happened? Any compatible documents to explain this?


Solution

As @Quentin said or another expert from http://social.msdn.microsoft.com/Forums/en-US/iewebdevelopment/thread/c1f546f6-d7e1-4b46-a1c9-8f02eaf1286b said, IE8 probably make rules strictly compared to IE6, and IE8 may treat it as an corrupt HTML document.

So, I decide to to use document.createElement to create elements dynamically instead of document.write, and insert these elements to DOM after some seconds delay. After some tests, it finally worked both in this test.html and real application.

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖树树初阳… 2024-12-13 20:23:13

据推测,尽管有命名空间,您仍将文档作为 text/html 提供。在 HTML 中,head 和 body 元素的开始和结束标记是可选的。头部内不允许有 H1 元素。

因此,当您在 end 内添加 document.write 和 H1 时,您会触发 head 的结束和 body 的开始。

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

Presumably, despite the namespace, you are serving the document as text/html. In HTML the start and end tags for the head and body element are optional. H1 elements are not allowed inside the head.

Thus, when you document.write and H1 inside the end, you trigger the end of the head and the start of the body.

I assume that IE then ignores the start tag for the body element as it would create a second body (which also isn't allowed).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文