プロが教えるわが家の防犯対策術!

htmlファイルのインクルード

あるページを複数の別のhtmlファイルを読み込んで構成しようと思っています。

以下がソースなのですがFirefoxでは

samplesample
テストテスト

と表示されるのですが、Internet Explorerでは

テストテスト

のみが表示されsample.htmlが読み込まれません。

何がいけないのでしょうか。
よろしくお願いします。

[index.html]
<html>
<head>
<title>テストページ</title>
<script type="text/javascript" src="prototype.js"></script>
<script type="text/javascript">
function my_include(id, file) {
document.open();
document.write('<div id="' + id + '"></div>');
document.close();

var options = {};
options.method = "get";
options.asynchronous = true;
new Ajax.Updater(id, file, options);
}
</script>
</head>
<body>
<div id="headers"></div>
<script type="text/javascript">my_include("headers","sample.html");</script>
テストテスト
</body>
</html>


[sample.html]
<head>
<title>sampleページ</title>
</head>
<body>
samplesample
</body>
</html>

[バージョン情報]
Firefox 3.6.3
Internet Explorer 8
prototype.js 1.6.1

A 回答 (2件)

ajaxは非同期に実行されています。

同じ関数の中にある
document.open();
document.write('<div id="' + id + '"></div>');
document.close();
のレンダリングが完了する前にレスポンスされているのでは?
new Ajax.Updater(id, file, options);
の前に
alert("ちょっと待つ");
を入れて試してみてはどうでしょう。

この回答への補足

alert("ちょっと待つ");
を入れてもIEではダメでした。
Firefoxでは大丈夫なんですよね。。

<html>
<head>
<title>テストページ</title>
<script type="text/javascript" src="prototype.js"></script>
<script type="text/javascript">
function my_include(id, file) {
document.open();
document.write('<div id="' + id + '"></div>');
document.close();
alert("ちょっと待つ");
var options = {};
options.method = "get";
options.asynchronous = true;
new Ajax.Updater(id, file, options);
}
</script>
</head>
<body>
<script type="text/javascript">my_include("headers","sample.html");</script>
テストテスト
</body>
</html>

補足日時:2010/05/10 23:24
    • good
    • 0
この回答へのお礼

自己解決しました。
prototype.jsはieだとローカルファイルにアクセスできないんですね。。
Webサーバ上にアップしたらできました。

初心者ですみません。
お騒がせしました。

お礼日時:2010/05/11 01:19

> document.write('<div id="' + id + '"></div>');



> <div id="headers"></div>

ID重複が原因じゃないでしょうか?
どちらかを消すと良いかもしれません。

この回答への補足

失礼しました。ソースが間違っていました。。

<div id="headers"></div>を消しても駄目でした。。

補足日時:2010/05/10 14:14
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!