4

IE6 では動作するが IE8 では動作しないサードパーティの Web アプリケーションがあります。

サンプル コードは次のようになります。「.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>

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 と実際のアプリケーションの両方で最終的に機能しました。

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>
4

1 に答える 1

4

おそらく、名前空間にもかかわらず、ドキュメントを text/html として提供しています。HTML では、head 要素と body 要素の開始タグと終了タグはオプションです。H1 要素は head 内では使用できません。

したがって、end 内で document.write と H1 を実行すると、head の end と body の start がトリガーされます。

IE は、body 要素の開始タグを無視して、2 番目の body を作成すると仮定します (これも許可されていません)。

于 2011-09-29T07:45:38.680 に答える