HTML DOM open() 方法

定義和用法
open() 方法打開一個輸出流來收集 document.write() 或 document.writeln() 方法輸出的內容。
調用 open() 方法打開一個新文檔並且用 write() 方法設置文檔內容後,必須記住用 document.close() 方法關閉文檔,並迫使其內容顯示出來。
注意:如果目標檔已經存在,它將被清除。如果這個方法沒有參數,會顯示一個新窗口(about:blank)。
語法
document.open(MIMEtype,replace)
參數 | 描述 |
---|---|
MIMEtype | 可選。規定正在寫的文檔的類型。默認值是 "text/html"。 |
replace | 可選。當此參數設置後,可引起新文檔從父文檔繼承歷史條目。 |
流覽器支持
所有主要流覽器都支持 open() 方法
實例
實例
打開一個輸出流並添加文本,然後關閉輸出流:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>IT研修(xuhuhu.com)</title>
<script>
function createDoc(){
var doc=document.open("text/html","replace");
var txt="<!DOCTYPE html><html><body>學習 HTML DOM 很有趣!</body></html>";
doc.write(txt);
doc.close();
}
</script>
</head>
<body>
<input type="button" value="新文檔" onclick="createDoc()">
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>IT研修(xuhuhu.com)</title>
<script>
function createDoc(){
var doc=document.open("text/html","replace");
var txt="<!DOCTYPE html><html><body>學習 HTML DOM 很有趣!</body></html>";
doc.write(txt);
doc.close();
}
</script>
</head>
<body>
<input type="button" value="新文檔" onclick="createDoc()">
</body>
</html>
實例 2
打開一個輸出流 (一個新窗口; about:blank),並添加文本,然後關閉輸出流:
<html>
<body>
<script>
var w=window.open();
w.document.open();
w.document.write("<h1>Hello World!</h1>");
w.document.close();
</script>
</body>
</html>
<body>
<script>
var w=window.open();
w.document.open();
w.document.write("<h1>Hello World!</h1>");
w.document.close();
</script>
</body>
</html>
