分享

window.onload,body onload和document.onreadysta...

 gideshi 2012-06-28

我们经常使用 window.onload 来处理页面,当页面加载完成做一些事情。但这个 window.onload 是页面全部加载完成,甚至包括图片

1. window.onload = function(){}

2. window.onload = functionName; // [color=red]注意:没有括号 

3. IE:
   window.attachEvent("onload",functionName);
   FF:
   window.addEventListener(); // 参数怎么写我忘了, 请自己搜索 

body onload="init();"事件是等doucment加载完成再加载相应的脚本
document.onreadstatechange()是指当对象状态变更时触发脚本


<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>

上面两种方式任何游览器都支持

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多