7

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

Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
arthur
  • 3,245
  • 4
  • 25
  • 34
  • 2
    I think this will help: http://stackoverflow.com/questions/4842432/jquery-recieve-document-ready-on-child-window – Joe Landsman Aug 26 '11 at 19:47
  • $(win.document).ready(function() { $(win.document).contents().find("...").doStuff(); }); ? this only performs when the document is ready... it 's not what i need .. i rather need to be notified when the window is fully loaded !! not as soon the DOM is ready .. thanks though.. – arthur Aug 26 '11 at 20:26
  • I just tested this out and it seemed to work. See the test script in my answer. – Joe Landsman Aug 26 '11 at 21:35

3 Answers3

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.

Joe Landsman
  • 2,177
  • 13
  • 9
  • i please repeat again : i need to have the alert('window is open') AFTER the window is loaded , NOT when the document of the window is READY... i know your script works, because i have already implemented it. But i need to have AFTER the window has been loaded... that#s why i used i my code above the $(window).load() function with "window" stand for the reference of the newly opened window. – arthur Aug 27 '11 at 10:33
0

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

David
  • 108
  • 1
  • 1
  • 9
0
$(document).ready(function() {
   //perform tasks, DOM is loaded
});

Hope this helps.

Grigor
  • 4,139
  • 10
  • 41
  • 79