判断页面及其中的iframe是否加载完毕

<script>
//判断页面加载情况
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
    window.onload = func;
} else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
}
}
function overWeb() {
alert('页面加载完毕!');
}
addLoadEvent(overWeb);
</script>

2.判断页面dom的部分元素是否加载完毕

<script>
//DOM-ready watcher
function domFunction(f, a)
{
//initialise the counter
var n = 0;

//start the timer
var t = setInterval(function()
{
   //continue flag indicates whether to continue to the next iteration
   //assume that we are going unless specified otherwise
   var c = true;

   //increase the counter
   n++;

   //if DOM methods are supported, and the body element exists
   //(using a double-check including document.body, for the benefit of older moz builds [eg ns7.1]
   //in which getElementsByTagName('body')[0] is undefined, unless this script is in the body section)
   if(typeof document.getElementsByTagName != 'undefined' && (document.getElementsByTagName('body')[0] != null || document.body != null))
   {
    //set the continue flag to false
    //because other things being equal, we're not going to continue
    c = false;

    //but ... if the arguments object is there
    if(typeof a == 'object')
    {
     //iterate through the object
     for(var i in a)
     {
      //if its value is "id" and the element with the given ID doesn't exist
      //or its value is "tag" and the specified collection has no members
      if
      (
       (a[i] == 'id' && document.getElementById(i) == null)
       ||
       (a[i] == 'tag' && document.getElementsByTagName(i).length < 1)
      )
      {
       //set the continue flag back to true
       //because a specific element or collection doesn't exist
       c = true;

       //no need to finish this loop
       break;
      }
     }
    }

    //if we're not continuing
    //we can call the argument function and clear the timer
    if(!c) { f(); clearInterval(t); }
   }
  
   //if the timer has reached 60 (so timeout after 15 seconds)
   //in practise, I've never seen this take longer than 7 iterations [in kde 3
   //in second place was IE6, which takes 2 or 3 iterations roughly 5% of the time]
   if(n >= 60)
   {
    //clear the timer
    clearInterval(t);
   }
  
}, 250);
};
function myFunction()
{
   alert("The DOM is ready");
};
var foobar = new domFunction(myFunction, {'iframe' : 'tag'});
</script>
<body>

<h1>domFunction</h1>
<iframe src="about:blank"></iframe><iframe src="about:blank"></iframe><iframe src="about:blank"></iframe>
<p id="poster"><img src="http://www.brothercake.com/scripts/iotbs/iotbs.bmp" alt="Invasion of the Body Snatchers poster (1956)" /></p>

</body>

3.用异步方法判断,其中的iframe是否加载完毕

<iframe src="http://www.baidu.com/" onreadystatechange="if(this.readyState=='complete')alert('IFRAME加载完成了!!');"></iframe>

但是:

iframe onreadystatechange的事件只用于ie5.5以上版本
frame不支持onreadystatechange的事件   
    
判断代码可以加在装载的页面内作判别
document.onreadystatechange = function(){
alert("load OK");
}

posted on 2009-04-15 09:07  Nick-fbx  阅读(5919)  评论(0编辑  收藏  举报

导航