11

I want to execute a function when an opened child window is closed. Child window opening code is:

outWin = window.open (pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=600, height=400, top=100, left=100);

I want to call a Javascript function written in parent window. I am using YUI-2 for developing the plugin. How can I make it work? What event should be registered?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Vinay Jeurkar
  • 3,054
  • 9
  • 37
  • 56

2 Answers2

33

You could do something like this.

var intervalID, childWindow;

childWindow = window.open("http://www.google.com");

function checkWindow() {
    if (childWindow && childWindow.closed) {
        window.clearInterval(intervalID);
        alert('closed');
    }
}
var intervalID = window.setInterval(checkWindow, 500);

References: window.setInterval and this answer.

Simple example on jsfiddle.

Community
  • 1
  • 1
Mark Coleman
  • 40,542
  • 9
  • 81
  • 101
  • 5
    I can't figure out why OPiazao's answer is marked as the correct answer here. THIS is the correct way to handle it based on the requirements described in the OP. – Carnix Oct 14 '14 at 21:37
4

You can try acces the parent window by:

window.opener.functionThatYouWant();

This code is inside de child window.

But if you open an window that the URL is in another domain (not localhost), you can't access it because of security issues.

I used this code on Firefox, I am not sure if it works crossbrowser.

JeanK
  • 1,765
  • 1
  • 15
  • 22
  • 1
    It works fine in Safari, but it didn't seem to work in chrome. – grc Mar 09 '11 at 01:47
  • 2
    Yeah, seems to have this problem. But the Mark's answer below seems better and it is crossbrowser, try it as well. – JeanK Mar 09 '11 at 01:54