这样就不转了
<html>
<script>
  function f() {
    document.write('in function f()');
document.close();
  }
</script>
  <body>
    <input type="button" onclick= "f();"/>
  </body>
</html>
这是为什么呢?

解决方案 »

  1.   

    关闭文档输出流就好了.
    在文档加载完成后,调用write方法,会隐式调用document.open方法来擦除当前文档并开始一个新的文档。所以要用close来关闭
    <html>
    <script>
      function f(){
         document.write('in function f()');
         document.close();
      }
      
    </script>
      <body>
        <input type="button" onclick= "f();"/>
      </body>
    </html>
      

  2.   

    看来IE与FF 对document.write 是不一样的? 
      

  3.   

    那是因为你没有把文档流关闭
    document.close();
    如果不是在解析html的时候用document. write的话,关闭是必须的
      

  4.   

    为什么在解析时不用关闭,在解析完以后就不用关闭
    例如<html> 
    <script>
      document.write('in function f()');  
    </script>
     <body> 
    </body> </html>这个是不用关闭的。
      

  5.   

    html代码也是要输出到浏览器的
    不能说是“解析时不用关闭”,我的理解是边解析边输出
    html结束后,流肯定关了。不关闭就是浪费资源
      

  6.   

    write MethodRes
    You should not use the write or writeln methods on the current document after the document has finished loading unless you first call the open method, which clears the current document's window and erases all variables.Note  When document.write or document.writeln is used in an event handler, document.close should also be used.DHTML 手册里讲滴很明白:
    当文档结束加载后你不应该对当前文档使用 write 或 writeln 方法,除非你首次调用 open 方法,
    open 方法会清除当前文档的窗口并删除全部变量。注意:当 document.write 或 document.writeln 被用于某个事件句柄时,同时也应该使用 document.close 方法。可见,在函数中调用 document.write 或 document.writeln 时必须调用 document.close 方法,当然 IE 可以容错,而 FF 则坚守原则!
    至于,
    <html> 
    <script>
      document.write('in function f()');  
    </script>
     <body> 
    </body> </html>
    这属于页面初次加载过程,完成时会自动调用 close 方法滴。