アプリ版:「スタンプのみでお礼する」機能のリリースについて

web上のjavascript入門ページなどを参照しながら学習中ですが詰まってしまいました。よろしくお願いします。

1. topページにセレクトボックスを設置。
2. セレクトされた内容に応じて、現在のウインドウを書き換えた後topページと同じセレクトボックスを設置。
3. 2を繰り返す。

このような動作をさせたいのですが、現在のウインドウを書き換えることは出来るのですがセレクトボックスを設置しようとするとエラーになります。

script_test01.htmlの内容
--------------------------------------------------
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-JP">
</head>
<body>
<script type='text/javascript' src='script_test01.js'>
</script>
<br>
<form name='form_0'>
<select onchange='rewrite()' name='sel_0'>
<option>A or B</option>
<option>A</option>
<option>B</option>
</select>
</body>
</html>
--------------------------------------------------

script_test01.jsの内容
--------------------------------------------------
function rewrite() {
var sel_in =document.form_0.sel_0.options.selectedIndex;
var sel_name = document.form_0.sel_0.options[sel_in].text;
document.write('<html>\n');
document.write('<head>\n');
document.write('<meta http-equiv="Content-Type" content="text/html; charset=EUC-JP">\n');
document.write("<script type='text/javascript' src='script_test01.js'>\n");
document.write("</script>\n");
document.write('</head>\n');
document.write('<body>\n');
document.write(sel_in,' : ',sel_name,'<br>\n');

/*↓この部分がうまくいかない
document.write('<br>\n');
document.write('<form name='form_0'>\n');
document.write('<select onchange='rewrite()' name='sel_0'>\n');
document.write('<option>A or B</option>\n');
document.write('<option>A</option>\n');
document.write('<option>B</option>\n');
document.write('</select>\n');
/*↑この部分がうまくいかない

document.write('</body>\n');
document.write('</html>\n');
}
--------------------------------------------------

script_test01.jsのコメントアウトをはずすとエラーになります。
script_test01.js内でさらに自分自身を記述している部分辺りなど、なにかおかしなことをやっていのではという自覚はありますが、よくわかりません。

あわせて質問ですが、.htmlと.jsはEUCで作成しcharsetもEUC-JPを指定しています。
topページをブラウザのエンコードで確認すると当然「日本語[EUC]」となっていますが、セレクトボックス選択後書き換えられたページを確認すると、「Unicode」なっていますがなぜでしょうか?「日本語[EUC]」に出来ないでしょうか?

A 回答 (1件)

document.write('<form name='form_0'>\n');


document.write('<select onchange='rewrite()' name='sel_0'>\n');
この2行。’の中に’が入っている。
\’のようにエスケープするか、”を使いましょう。

後、document.writeの前後のopen/closeを行いましょう。
特にcloseが無い為、永遠に読み続けるのが鬱陶しいです。


文字コードに関しては、JavaScriptは内部処理でunicodeを使用している為。
documentのopen後に、「document.charset='EUC-JP';」のような指定をすることで解消できます。

この回答への補足

ありがとうございます。
>\’のようにエスケープするか、”を使いましょう。
ああっ!こんなことわかってたはずなのに・・・
文字コードの方も試してみましたがバッチリです。
書き換えた方のセレクトボックスがうまく動作しませんが、もう少し考えてみます。

補足日時:2008/06/12 15:09
    • good
    • 0
この回答へのお礼

ありがとうございました。
一発解決です。
open/closeに関する指摘もありがたかったです。

お礼日時:2008/06/12 15:23

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