document = document . open
( [ type [, replace ] ] )
Causes the Document
to be replaced in-place, as if it was a new Document
object, but reusing the previous object, which is then returned.
If the type argument is omitted or has the value "text/html
", then the resulting Document
has an HTML parser associated with it, which can be given data to parse using document.write()
. Otherwise, all content passed to document.write()
will be parsed as plain text.
If the replace argument is present and has the value "replace
", the existing entries in the session history for the Document
object are removed.
The method has no effect if the Document
is still being parsed.
Throws an InvalidStateError
exception if the Document
is an XML document.
以上是html5手册中的解释。
当type省略或是“text/html",该语句就会返回一个带有html解析的文档对象,使用document.write()方法将要被解析的html内容写入到当前文档中进行解析。
如果replace 参数使用了"replace" ,就会将原来的文档移除。
当文档正在被解析的时候,这个方法不会起作用。也就是说,下面的代码是不会产生本该有的输出
<!DOCTYPE html>
<html lang="en">
<head>
<title>test<title>
</head>
<body>
<p>beginer</p>
<script>
var new=document.open("text/html");
new.write("<p> this is new content</p>");
new.close();
</script>
</body>
</html>
即输出的内容是:
beginer
this is new content
而不是想象中的:
this is new content
该方法的另外一个作用是打开文档:
open
(
url,
name,
features [,
replace ] )
Works like the
window.open()
method.
eg. <script>
document.open("http://www.baidu.com","","_self");
window.open("http://www.baidu.com","","_self");
</script>
的作用是打开百度页面;
与window.open("http://www.baidu.com","","_self");
的作用相同.