admin管理员组

文章数量:1323225

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.

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.

Share Improve this question edited Jan 2, 2023 at 14:16 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked May 5, 2010 at 13:49 biluriudaybiluriuday 4281 gold badge7 silver badges16 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9

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.", "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.", "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.", "OtherWindow");
setTimeout(function timeout() {
  if (w.closed) {
    // code that you want to run when window closes
  }
  else
    setTimeout(timeout, 100);
}, 100);

本文标签: dom eventsHow to call a JavaScript function of parent window when child is closedStack Overflow