开发者

DOM - More than 5 'window' objects on one page

I'm trying to extract data from o开发者_Python百科ne of my websites using Greasemonkey. Problem is, the script runs 6 times because apparently the page loads content from 6 different servers.

So if I put in an alert ("Hey"); the code runs 6 times and I get 6 alerts.

How can I wait for the entire page to load and then start playing with the DOM. Also, I'm using jQuery.

Thanks


Problem is, the script runs 6 times because apparently the page loads content from 6 different servers.

Do you mean that the page is loading other pages via iFrames? as far as I know that is the only way that what you explain can happen.. So if that is true, try:

(function(){
  //ignore any window/iframe that is not the top window
  if(window.parent != window) return;

  // the main function
  var mainFunction = function(){
    alert("Hey");
  }

  if(document.readyState=="complete"){
    // for google chrome
    mainFunction();
  }
  else{
    // wait until the load event
    window.addEventListener("load", mainFunction, false);
  }
})();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜