views:

288

answers:

3
 window.popup = window.open($(this).attr('href'), 'Ad', 'left=20,top=20,width=500,height=500,toolbar=1,resizable=0');
 $(window.popup).onload = function()
        {
                alert("Popup has loaded a page");
        };

This doesn't work in any browser I've tried it with (IE, Firefox, Chrome). How can I detect when a page is loaded in the window (like an iframe onload)?

+1  A: 
var myPopup = window.open(...);
myPopup.addEventListener('load', myFunction, false);

If you care about IE, use the following as the second line instead:

myPopup[myPopup.addEventListener ? 'addEventListener' : 'attachEvent'](
  (myPopup.attachEvent ? 'on' : '') + 'load', myFunction, false
);

As you can see, supporting IE is quite cumbersome and should be avoided if possible. I mean, if you need to support IE because of your audience, by all means, do so.

Delan Azabani
Anyway I can do this cross-domain?
Chris T
Unfortunately not. For security reasons, browsers disable JavaScript object interaction across different domains.
Delan Azabani
This won't work in all browsers.
Tim Down
Then just use a tertiary conditional. I'll add it.
Delan Azabani
+1 Tim, this won't work in IE; Tim's answer above is correct.
jvenema
A: 

onload event handler must be inside popup's HTML <body> markup.

Thevs
+3  A: 

If the pop-up's document is from a different domain, this is simply not possible.

If not, there's still no way you'll be able to make this work cross-browser without some help from the document being loaded into the pop-up. You need to be able to detect a change in the pop-up that occurs once it has loaded, which could be a variable that JavaScript in the pop-up page sets when it handles its own load event, or if you have some control of it you could add a call to a function in the opener.

Tim Down
A simple way to check it to monitor the availability of a JS function in the other page in a setTimeout() call.
jvenema