HTML DOM open() 方法


定义和用法

open() 方法打开一个输出流来收集 document.write() 或 document.writeln() 方法输出的内容。

调用 open() 方法打开一个新文档并且用 write() 方法设置文档内容后,必须记住用  document.close() 方法关闭文档,并迫使其内容显示出来。

注意:如果目标文件已经存在,它将被清除。如果这个方法没有参数,会显示一个新窗口(about:blank)。

语法

  1. document.open(MIMEtype,replace)


参数描述
MIMEtype可选。规定正在写的文档的类型。默认值是 "text/html"。
replace可选。当此参数设置后,可引起新文档从父文档继承历史条目。

浏览器支持

Internet ExplorerFirefoxOperaGoogle ChromeSafari

所有主要浏览器都支持 open() 方法

实例

实例

打开一个输出流并添加文本,然后关闭输出流:

  1. <html>
    <head>
    <script>
    function createDoc()
      {
      var doc=document.open("text/html","replace");
      var txt="<html><body>Learning about the HTML DOM is fun!</body></html>";
      doc.write(txt);
      doc.close();
      }
    </script>
    </head>

    <body>
    <input type="button" value="New document" onclick="createDoc()">
    </body>
    </html>
运行一下 »

实例 2

打开一个输出流 (一个新窗口; about:blank),并添加文本,然后关闭输出流:

  1. <html>
    <body>

    <script>
    var w=window.open();
    w.document.open();
    w.document.write("<h1>Hello World!</h1>");
    w.document.close();
    </script>

    </body>
    </html>
运行一下 »