document 是 window 與 frames 物件的屬性,代表顯示於視窗或框架中的一份文件,範圍涵蓋 <head> 到 </body> 的全部內容。
用法:document(目前視窗)或 <視窗物件>.document(指定視窗)。
title:文件標題,等同 HTML 的 <title>;在 Netscape 中不接受賦值bgColor:背景色,對應 <body bgcolor>fgColor:文字前景色,對應 <body text>linkColor:未點擊過的連結顏色alinkColor:作用中(焦點所在)連結的顏色vlinkColor:已造訪過的連結顏色URL:設定此屬性可在同一視窗開啟另一網頁charset:字元集,簡體中文通常為 gb2312cookie:讀寫 CookielastModified:目前文件最後修改日期referrer:若目前文件是點連結開啟的,回傳來源網址fileCreatedDate、fileModifiedDate、fileSize:皆為唯讀屬性open():開啟文件以便寫入資料,通常不必手動呼叫,JavaScript 會自動處理write() / writeln():向文件寫入內容,寫入的內容會被視為 HTML 處理。兩者的差別在於 writeln() 結束時會附加一個換行;這個換行是否呈現在畫面上,取決於插入位置——若寫在 <pre> 標籤中,換行就會被保留clear():清空目前文件close():關閉文件並停止寫入。若用過 write()、writeln() 或 clear(),就必須呼叫 close() 才能確保變 rõng容被呈現;若 script 是嵌在文件中間執行,則不必呼叫<script language="JavaScript">
var whatsNew = open('', '_blank', 'top=50,left=50,width=200,height=300,'
+ 'menubar=no,toolbar=no,directories=no,location=no,'
+ 'status=no,resizable=no,scrollbars=yes');
whatsNew.document.write('<center><b>更新通知</b></center>');
whatsNew.document.write('<p>最後更新日期:00.08.01');
whatsNew.document.write('<p>00.08.01:新增「我的最愛」欄目。');
whatsNew.document.write('<p align="right">'
+ '<a href="javascript:self.close()">關閉視窗</a>');
whatsNew.document.close();
</script>
這裡同時用到了 open() 開視窗、newWindow.document.write() 寫入內容、以及 close() 收尾。當然,更實際的做法是先寫好一個靜態 HTML 檔,再用 open() 直接載入它——這樣內容與程式分離,維護起來清爽得多。