document.open() 與 document.write()的區(qū)別
更新時間:2007年08月13日 19:53:13 作者:
document.open() 打開一個新的空白文檔,在IE下,open有兩個默認參數(shù),相當于document.open("text/html",'""),第二個參數(shù)只有一個值可選:replace,如果啟用了該值,則新建的文檔會覆蓋當前頁面的文檔(相當于清空了原文檔里的所有元素,且不能后退即,瀏覽器的后退按鈕不可用);
看一個例子:
<SCRIPT LANGUAGE="JavaScript">
<!--
function test(){
document.open("text/html","replace");
document.writeln(Math.random());
document.write("<input type='button' value='back(第二個按鈕)' onclick='history.back()'>")
document.close();
document.open("text/html","");
document.writeln(Math.random());
document.write("<input type='button' value='back(第三個按鈕)' onclick='history.back()'>")
document.close();
document.open("text/html","");
document.writeln(Math.random());
document.write("<input type='button' value='back(第四個按鈕)' onclick='history.back()'>")
document.close();
}
//-->
</SCRIPT>
<input type="button" value="第一個按鈕" onclick="test()">
平常都不寫document.open() 與 document.close(),因為瀏覽器會在write之前先open一個文檔,再把write的內容輸出到原文檔里面。write結束后,默認是不會有close的,否則第二行document.write的時候就會覆蓋之前的write。
看一個例子:
<SCRIPT LANGUAGE="JavaScript">
<!--
function test(){
document.open("text/html","replace");
document.writeln(Math.random());
document.write("<input type='button' value='back(第二個按鈕)' onclick='history.back()'>")
document.close();
document.open("text/html","");
document.writeln(Math.random());
document.write("<input type='button' value='back(第三個按鈕)' onclick='history.back()'>")
document.close();
document.open("text/html","");
document.writeln(Math.random());
document.write("<input type='button' value='back(第四個按鈕)' onclick='history.back()'>")
document.close();
}
//-->
</SCRIPT>
<input type="button" value="第一個按鈕" onclick="test()">
平常都不寫document.open() 與 document.close(),因為瀏覽器會在write之前先open一個文檔,再把write的內容輸出到原文檔里面。write結束后,默認是不會有close的,否則第二行document.write的時候就會覆蓋之前的write。
相關文章
JavaScript中字符串與Unicode編碼互相轉換的實現(xiàn)方法
這篇文章主要介紹了JavaScript中字符串與Unicode編碼互相轉換的實現(xiàn)方法涉及JavaScript編碼、數(shù)據(jù)類型等的轉換技巧,需要的朋友可以參考下2015-12-12JavaScript中valueOf函數(shù)與toString方法深入理解
基本上,所有JS數(shù)據(jù)類型都擁有valueOf和toString這兩個方法,null除外。它們倆解決javascript值運算與顯示的問題,本文將詳細介紹,有需要的朋友可以參考下2012-12-12