Jquery: how to check if a newly opened window has been fully loaded? / window loads an external page (like www.yahoo.com)

岁酱吖の 提交于 2019-12-18 07:46:19

问题


the following doesn't work , why ?

var myWindow=null;
myWindow = window.open(targetUrlVar,"_blank","resizable=yes");
$(myWindow).load(function(){
    alert('hello');
});

Though MyWindow is a Window reference, no check is performed to see whether or not it has been fully loaded. i thought $(window).load(...) would work here for "window " being replaced by "MyWindow".

the following works:

$(myWindow).load(function(){
    alert('hello');
});

for targetUrlVar being an internal resource (like a page belonging to my domain) it works.. but as soon as i wanted to used the .load() or myWindow.onload() with a targetUrlVar being an external page (such as www.yahoo.com or www.google.com ), it doesn't work any more... No alert is displayed..

i need you help... thank you everyone


回答1:


If you want to trigger the alert when the child window is open regardless of the state of the child window's DOM then this should work. I also provided a way to test this assumption.

Create a test PHP script (or similar scripting language) with the following content:

<html>
<head><title>test</title></head>
<body>
    <?php sleep(5); // Sleep to delay DOM from loading ?>

    Done sleeping...
</body>
</html>

Then call this test page as your child window usingthe following javascript:

win = window.open('test.php', 'test', 'width=300, height=400, x=800');
win.focus();
$(win.document).ready(function() {
    alert('Window is open');
});

You'll notice that the alert() from the parent window fires before you see "Done sleeping..." appear in the child window.




回答2:


$(document).ready(function() {
   //perform tasks, DOM is loaded
});

Hope this helps.




回答3:


I see this question is somewhat old.

However, arthur stated,

for targetUrlVar being an internal resource (like a page belonging to my domain) it works.. but as soon as i wanted to used the .load() or myWindow.onload() with a targetUrlVar being an external page (such as www.yahoo.com or www.google.com ), it doesn't work any more... No alert is displayed..

I think that the issue may be due to the targetUrlVar being external; the alert may be blocked to prevent cross-site scripting (aka XSS).



来源:https://stackoverflow.com/questions/7209745/jquery-how-to-check-if-a-newly-opened-window-has-been-fully-loaded-window-l

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!