0

In my app, when a user clicks a button I open a window:

myWindow = window.open('http://www.google.com')

When I'm done processing myWindow, I clean-up the environment so that subsequent clicks on the same button will open a new window (if I do not do this, subsequent clicks do nothing):

myWindow.close();
delete window.myWindow;

Is it possible to close then remove myWindow from the environment if the user manually exits the window? If they do so now, subsequent clicks do nothing unless the page is refreshed.

Chris Walsh
  • 3,423
  • 2
  • 42
  • 62
datasci
  • 1,019
  • 2
  • 12
  • 29
  • The beforeunload event can be used to message back to the calling window via the window.opener property (check it still exists first). The calling window can then do its own clean up, however it won't need to issue `myWindow.close()` as this already in progress. – Chris Walsh Jun 03 '15 at 22:59

1 Answers1

0

The "beforeunload" event doesn't appear to work when the window crosses domains (Capture the close event of popup window in JavaScript, Javascript detect closing popup loaded with another domain).

However, this appears to detect that the window has been closed by the user:

if (myWindow.closed) {
     delete window.myWindow;
}

Wrap it in a timer (as in the second link referenced above), and it appears to solve the problem.

Community
  • 1
  • 1
datasci
  • 1,019
  • 2
  • 12
  • 29