close() 方法可關閉1個由 document.open 方法打開的輸出流,并顯示選定的數據。
語法:
document.close()
該方法將關閉 open() 方法打開的文檔流,并強迫地顯示出所有緩存的輸出內容。如果您使用 write() 方法動態地輸出1個文檔,必須記住當你這么做的時候要調用 close() 方法,以確保所有文檔內容都能顯示。
1旦調用了 close(),就不應當再次調用 write(),由于這會隱式地調用 open() 來擦除當前文檔并開始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>