开发者

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 ca开发者_如何转开发ll 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.


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


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


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);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜