dポイントプレゼントキャンペーン実施中!

ホームページで各ページに共通したヘッダーを
JavaScriptのdocument.write()を利用してつけようとしています。
OSはwindows Xpで、制作ツールは、alphaEDITを使用しています。

1.「header.js」ファイルを用意
document.write("<header.html>header");

2.「index.html」を用意
<html>
<head></head>
<body>
<script language="JavaScript" src="header.js"></script>
  中身
</body>
</html>

3.スタイルシートsample3.cssを用意

4.header.html を用意
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>ヘッダー</title>
<meta content="text/html; charset=shift_jis" http-equiv="Content-Type">
<link rel="stylesheet" type="text/css" href="sample3.css">
<script language="JavaScript" src="func.js"></script>
</head>
<body>
(かなり長いので省略)
<p></p></body></html>

これで「index.html」を実行しますと、
  header 
  中身
と表示されます。

headerという文字ではなく、header.html 全部を表示し、かつスタイルシートも反映させたいのですが、どうすればいいですか?
教えて下さい。

参考にしたURL
各ページに共通したヘッダー(フッター)をつける
http://tagland.net/technique/common_header.html

A 回答 (2件)

私はJavaScriptでやっていますよ。



【index.html】
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<META http-equiv="Pragma" content="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">
</head>
<script LANGUAGE="JavaScript1.2" src="script/header.js"></script>
<center>
<table border=0 width=800>
<tr>
<td>
<script LANGUAGE="JavaScript1.2" src="topicks.js"></script>
・・・以下本体部分省略・・・

【header.js の内容】
<!--
document.write("<body text=teal leftmargin=0 topmargin=0 marginwidth=0 marginheight=0>\n");
document.write("<table border=0 hspace=0 vspace=0 cellspacing=0 cellpadding=0 width=100%e>\n");
document.write("<tr><td bgcolor=mediumaquamarine width=100%>\n");
document.write("<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor=white>\n");
document.write("<tr>\n");
document.write("<td><img src=images/spur_img2.gif width=114 height=57 border=0 alt=Spurトップページへ></a></td>");
document.write("<td align=center>\n");
document.write("<table border=0>\n");
document.write("<tr valign=middle>\n");
・・・以下省略・・・

つまり、index.htmlで外部ファイルの「header.js」を挿入したい位置で指定する。
挿入するHTML文を、「header.js」の中に document.write で書き連ねる。

以上です。
    • good
    • 0
この回答へのお礼

ご回答ありがとうございました。
やはり、下記のように1行の記述で全体のheader.html
を書き出すというのは、出来ないのですね。
「header.js」ファイルを用意
document.write("<header.html>header");

「header.js」の中に document.write で書き連ねると数十行になりますが、
コツコツと記入していかないと駄目なのですね。

お礼日時:2011/07/31 23:30

>document.write("<header.html>header");



その書き方では、「<header.html>header」という文字を出力することになります。
document.write("");
では、””で囲まれた部分の 文字列 を出力します。
    • good
    • 0
この回答へのお礼

ご回答ありがとうございました。
言われてみて納得しました。
まだ、write の使い方がよくわかっていなかったようです。
header.htmlの内容全部を1行位で書き出す方法は
JavaScript では、できないのですね。 

お礼日時:2011/08/03 23:56

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