document.open() 與 document.write()的區(qū)別
更新時(shí)間:2007年08月13日 19:53:13 作者:
document.open() 打開(kāi)一個(gè)新的空白文檔,在IE下,open有兩個(gè)默認(rèn)參數(shù),相當(dāng)于document.open("text/html",'""),第二個(gè)參數(shù)只有一個(gè)值可選:replace,如果啟用了該值,則新建的文檔會(huì)覆蓋當(dāng)前頁(yè)面的文檔(相當(dāng)于清空了原文檔里的所有元素,且不能后退即,瀏覽器的后退按鈕不可用);
看一個(gè)例子:
<SCRIPT LANGUAGE="JavaScript">
<!--
function test(){
document.open("text/html","replace");
document.writeln(Math.random());
document.write("<input type='button' value='back(第二個(gè)按鈕)' onclick='history.back()'>")
document.close();
document.open("text/html","");
document.writeln(Math.random());
document.write("<input type='button' value='back(第三個(gè)按鈕)' onclick='history.back()'>")
document.close();
document.open("text/html","");
document.writeln(Math.random());
document.write("<input type='button' value='back(第四個(gè)按鈕)' onclick='history.back()'>")
document.close();
}
//-->
</SCRIPT>
<input type="button" value="第一個(gè)按鈕" onclick="test()">
平常都不寫(xiě)document.open() 與 document.close(),因?yàn)闉g覽器會(huì)在write之前先open一個(gè)文檔,再把write的內(nèi)容輸出到原文檔里面。write結(jié)束后,默認(rèn)是不會(huì)有close的,否則第二行document.write的時(shí)候就會(huì)覆蓋之前的write。
看一個(gè)例子:
<SCRIPT LANGUAGE="JavaScript">
<!--
function test(){
document.open("text/html","replace");
document.writeln(Math.random());
document.write("<input type='button' value='back(第二個(gè)按鈕)' onclick='history.back()'>")
document.close();
document.open("text/html","");
document.writeln(Math.random());
document.write("<input type='button' value='back(第三個(gè)按鈕)' onclick='history.back()'>")
document.close();
document.open("text/html","");
document.writeln(Math.random());
document.write("<input type='button' value='back(第四個(gè)按鈕)' onclick='history.back()'>")
document.close();
}
//-->
</SCRIPT>
<input type="button" value="第一個(gè)按鈕" onclick="test()">
平常都不寫(xiě)document.open() 與 document.close(),因?yàn)闉g覽器會(huì)在write之前先open一個(gè)文檔,再把write的內(nèi)容輸出到原文檔里面。write結(jié)束后,默認(rèn)是不會(huì)有close的,否則第二行document.write的時(shí)候就會(huì)覆蓋之前的write。
您可能感興趣的文章:
- js中document.write使用過(guò)程中的一點(diǎn)疑問(wèn)解答
- js document.write()使用介紹
- document.write()及其輸出內(nèi)容的樣式、位置控制
- document.write與writeln的輸出內(nèi)容區(qū)別說(shuō)明
- 在網(wǎng)頁(yè)中使用document.write時(shí)遭遇的奇怪問(wèn)題
- 第一個(gè)JavaScript入門(mén)基礎(chǔ) document.write輸出
- 代碼生成器 document.write()
- document.open() 與 document.write()
- JS 中document.write()的用法和清空的原因淺析
相關(guān)文章
E3 tree 1.6在Firefox下顯示問(wèn)題的修復(fù)方法
tree 在Firefox下只顯示一句話(huà),用firebug查看頁(yè)面元素觀察發(fā)現(xiàn),兩個(gè)script導(dǎo)入被一個(gè)<script>分隔開(kāi)了,顯然是document.write的問(wèn)題.由于Firefox對(duì)js規(guī)范的檢查比較嚴(yán)格,肯定一些字符輸出的的時(shí)候沒(méi)有轉(zhuǎn)義2013-01-01window.open()詳解及瀏覽器兼容性問(wèn)題示例探討
這篇文章主要介紹了window.open()的使用及瀏覽器兼容性問(wèn)題方面的知識(shí),感興趣的朋友可以參考下2014-05-05JavaScript中字符串與Unicode編碼互相轉(zhuǎn)換的實(shí)現(xiàn)方法
這篇文章主要介紹了JavaScript中字符串與Unicode編碼互相轉(zhuǎn)換的實(shí)現(xiàn)方法涉及JavaScript編碼、數(shù)據(jù)類(lèi)型等的轉(zhuǎn)換技巧,需要的朋友可以參考下2015-12-12JavaScript中valueOf函數(shù)與toString方法深入理解
基本上,所有JS數(shù)據(jù)類(lèi)型都擁有valueOf和toString這兩個(gè)方法,null除外。它們倆解決javascript值運(yùn)算與顯示的問(wèn)題,本文將詳細(xì)介紹,有需要的朋友可以參考下2012-12-12