3

非常に単純な Javascript を含む小さな HTML Web ページを作成した後、円が回転し続けていることに気付きました (firefox)。他のページで何度も見たことがあり、Javascript のエラーかループの問題だといつも思っていましたが、実行していたこのプログラムは間違いなく完了していました。

これを行う小さな Web ページの例を次に示します。ボタンをクリックして処理が完了すると、throbber (この用語については Kooilinc に感謝します) が続行されます。

<!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>Grade Tester</title>
    <script language="javascript" type="text/javascript">       
      function calculate()
      {
       var grade = document.getElementById("grade").value.toUpperCase();        
        switch (grade)
        {
         case "A":
           document.write("Outstanding Achievement");
           break;

         case "B":
           document.write("Above Average");
           break;

         case "C":
           document.write("Average Achievement");
           break;

          case "D":
            document.write("Low Passing Grade");
           break;

         case "F":
           document.write("Failing Grade");
            break;
        }
      }
    </script>       
    </head>
  <body>
    Grade:<input type="text" id="grade" />
    <br /><input type="button" value="Get Result" onclick="calculate()" />
  </body>
</html>

停止を押すことで停止できました(右クリックして停止)。

スクリプトの実行後に Javascript が処理を停止する (またはブラウザが処理を停止する) にはどうすればよいですか?

注 1: これは、IE や Chrome ではなく、FF4.01 で発生しました (このコード サンプルの場合)。

注 2: switch ステートメントの後で window.stop() 関数を試しましたが、throbber は停止しませんでした。

Lekensteyn の回答によると、switch ステートメントの後の document.close() によってこれが解決されましたが、これはまったくドキドキせず、document.close() を使用しない例です。

<!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>Lab 9.1</title>
  </head>
  <body>
    <script language="javascript" type="text/javascript">
      var phrase = "Every good boy does fine goes on the line."
      document.write(phrase.length + "<br />");
      for(var i = 0; i < phrase.length; i++)
      {
        document.write(phrase.charCodeAt(i) + " ");
      }
        document.write("<br />");
        document.write(phrase.toLowerCase() + "<br />");
        document.write(phrase.toUpperCase() + "<br />");
    </script>
  </body>
</html>
4

1 に答える 1

1

ドキュメントが以前に閉じられていなかった場合は、 を呼び出した後document.write()、 を呼び出す必要があります。document.close()

基本的に、これは起こります:

  1. ページが解析されています
  2. スクリプトが処理されます
  3. ページは「閉鎖」されています ( document.close())

ステップ 2 内で が呼び出された場合document.write()、ステップ 3 で終了します。ステップ 3 を超えて、 を呼び出すとdocument.write、ページが再び開かれますが、ページは FF で閉じません (他についてはわかりません)。を呼び出して閉じる必要がありdocument.close()ます。

ウィンドウを開いてFFですばやく書き込む必要があるときに、これを発見しました。

var win = window.open();
win.document.write("something");
// the next line is required to finish the page
win.document.close();

同じことがあなたのケースにも当てはまります:

<script type="text/javascript">
function calculate()
{
    var grade = document.getElementById("grade").value.toUpperCase();
    switch (grade)
    {
    case "A":
        document.write("Outstanding Achievement");
        break;
    // removed some cases
    case "F":
        document.write("Failing Grade");
        break;
    }
    // finish the document
    document.close();
}
</script>
于 2011-05-20T16:15:11.910 に答える