我們經(jīng)常使用 window.onload 來處理頁面,當(dāng)頁面加載完成做一些事情。但這個 window.onload 是頁面全部加載完成,甚至包括圖片
1. window.onload = function(){}
2. window.onload = functionName; // [color=red]注意:沒有括號
3. IE: window.attachEvent("onload",functionName); FF: window.addEventListener(); // 參數(shù)怎么寫我忘了, 請自己搜索
body onload="init();"事件是等doucment加載完成再加載相應(yīng)的腳本 document.onreadstatechange()是指當(dāng)對象狀態(tài)變更時觸發(fā)腳本
<script type="text/javascript"> function init() { // quit if this function has already been called if (arguments.callee.done) return;
// flag this function so we don't do the same thing twice arguments.callee.done = true;
// create the "page loaded" message var text = document.createTextNode("Page loaded!"); var message = document.getElementById("message"); message.appendChild(text); };
/* for Mozilla */ if (document.addEventListener) { document.addEventListener("DOMContentLoaded", init, null); }
/* for Internet Explorer */ /*@cc_on @*/ /*@if (@_win32) document.write("<script defer src=ie_onload.js><"+"/script>"); /*@end @*/
/* for other browsers */ window.onload = init; </script> <p id="message"></p>
示例 <script for=window event=onload> function inint(){ alert("文檔加載完成") } </script>
<script language="Javascript"> function document.onreadystatechange() { DoLayout(); window.onresize = DoLayout;
Composition.document.open() Composition.document.write("<head><style type=\"text/css\">body {font-size: 10.8pt}</style><meta http-equiv=Content-Type content=\"text/html; charset=gb2312\"></head><BODY bgcolor=\"#FFFFFF\" MONOSPACE></body>"); Composition.document.close() Composition.document.designMode="On" } </script> 這兩種加載腳本的方式只針對IE游覽器才有效
<script type="text/javascript"> function init(){ alert("頁面加載完畢!"); } window.onload=init; </script>
<html> <body onload="init()"> </body> </html> 上面兩種方式任何游覽器都支持
|