biluriuday
biluriuday

Reputation: 428

How to call a JavaScript function of parent window when child is closed?

I am creating a pop-up window. After I am finished with the work on child (pop up) window and click close button, I need to call a JavaScript function of the parent window. How can I achieve this? I am not creating the child window myself but displaying the contents of some other URL.

Upvotes: 4

Views: 2747

Answers (3)

Pointy
Pointy

Reputation: 413720

I don't think you can get an event, because you can't mess with the document itself when the URL is from a different domain. You can however poll and check the "closed" property of the window object:

var w = window.open("http://what.ever.com", "OtherWindow");
setTimeout(function() {
  if (w.closed) {
    // code that you want to run when window closes
  }
  else
    setTimeout(arguments.callee, 100);
}, 100);

You could also start an interval timer if you prefer:

var w = window.open("http://what.ever.com", "OtherWindow");
var interval = setInterval(function() {
  if (w.closed) {
    // do stuff
    cancelInterval(interval);
  }
}, 100);

Upvotes: 9

David Murdoch
David Murdoch

Reputation: 89322

Don't vote for this. It is just an improvement of Pointy's code to getting rid of arguments.callee. Vote for Pointy.

var w = window.open("http://what.ever.com", "OtherWindow");
setTimeout(function timeout() {
  if (w.closed) {
    // code that you want to run when window closes
  }
  else
    setTimeout(timeout, 100);
}, 100);

Upvotes: 0

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114367

If the child window is not originating from the same domain name as the parent window, you're locked out due to the same origin policy. This is done deliberately to prevent cross-site-scripting attacks (XSS).

Upvotes: 0

Related Questions