JavaScriptで、次のような出力を得られる命令(具体的なコード)を教えてください。


「 1/index.txt
の内容を、そのままコピーして持ってくる」

です。よろしくお願い申し上げます。

回答の条件
  • 1人2回まで
  • 登録:
  • 終了:2011/05/11 02:25:03
※ 有料アンケート・ポイント付き質問機能は2023年2月28日に終了しました。

回答3件)

id:TYCOON No.2

回答回数60ベストアンサー獲得回数3

ポイント16pt

document.getElementById("id名").innerText=index;

とか。コピーしてもってくるというかそのまま反映させるというか。


http://faq.creasus.net/02/0801/

id:lunlumo No.3

回答回数107ベストアンサー獲得回数14

ポイント48pt

サーバ上の"1/index.txt"の内容をページ上にそのまま表示したい,との理解で良かったでしょうか。であれば,こんな感じでしょうか。(windofjulyさんとTYCOONさんの回答を一つにまとめただけのコードになりますが...。)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <title>JavaScript Test</title>
    <script>
      function createHttpRequest() {
        var request = null;
        try {
          request = new XMLHttpRequest();
        } catch(e) {
          try {
            request = new ActiveXObject("Msxml2.XMLHTTP");
          } catch (e) {
            request = new ActiveXObject("Microsoft.XMLHTTP");
          }
        }
        return request;
      }
      window.onload = function() {
          var request = createHttpRequest();
          request.open('get', '1/index.txt', true);
          request.onreadystatechange = function() {
              if (request.readyState == 4) {
                var index = document.getElementById('index');
                if (request.status != 200) {
                  index.innerHTML = 'エラーが発生しました';
                } else {
                  try {
                  index.innerText = request.responseText;
                  } catch (e) {
                    index.innerHTML = request.responseText;
                  }
                }
              }
            };
          request.send('');
        }
    </script>
  </head>
  <body>
    <div>
        <pre id="index"></pre>
    </div>
  </body>
</html>
id:Web-Production

ありがとうございます。具体的に示していただき、大変助かりました。

2011/05/05 18:34:16

コメントはまだありません

この質問への反応(ブックマークコメント)

「あの人に答えてほしい」「この質問はあの人が答えられそう」というときに、回答リクエストを送ってみてましょう。

これ以上回答リクエストを送信することはできません。制限について

回答リクエストを送信したユーザーはいません