open() 方法可打開1個新文檔,并擦除當前文檔的內容。
語法:
document.open(mimetype,replace)
mimetype 可選。規定正在寫的文檔的類型。默許值是 "text/html"。
replace 可選。當此參數設置后,可引發新文檔從父文檔繼承歷史條目。
該方法將擦除當前 HTML 文檔的內容,開始1個新的文檔,新文檔用 write() 方法或 writeln() 方法編寫。
調用 open() 方法打開1個新文檔并且用 write() 方法設置文檔內容后,必須記住用 close 方法關閉文檔,并迫使其內容顯示出來。
屬于被覆蓋的文檔的1部份的腳本或事件句柄不能調用該方法,由于腳本或事件句柄本身也會被覆蓋。
例子:
<html>
<head>
<script type="text/javascript">
function createNewDoc()
{
var newDoc=document.open("text/html","replace")
;
var txt="<html><body>Learning about the DOM is FUN!</body></html>";
newDoc.write(txt);
newDoc.close();
}
</script>
</head>
<body>
<input type="button" value="Write to a new document"
onclick="createNewDoc()">
</body>
</html>