1

I have a site that opens another tab/window temporarily. Once it's no longer relevant or needed, its script calls window.close(). However, the user may occasionally have that URL bookmarked or access it in some other way manually rather than arriving there as the result of the first/main window programmatically opening it.

In such cases, I note that the .close() call results in a warning on the console (Chrome 48). I understand it's a security feature, and am totally fine with that.

What I'd like to do is to detect that the call didn't work (or won't) and do something else instead - in my case show the user a flash message saying they should close the tab/window manually.

I tried the below code, but am realizing that since no exception was thrown, the catch is never hit. Is there a way to do what I'm wanting to?

try {
  window.close();
}
catch (e) {
  showFlashMessage('Error closing tab.  Please close it manually.'); // Never gets here :(
}
1
  • MDN doesn't give clues but MSDN claims there's a return value to check. No idea if it's an IE-only feature. Commented Mar 8, 2016 at 15:34

1 Answer 1

1

I guess you should be able to detect whether your window is a popup or not by inspecting window.opener. If there is no such other window, you probably can't .close() yourself.

Sign up to request clarification or add additional context in comments.

2 Comments

Please note: Windows Phone browser does not support window.opener (tested with Microsoft Edge 25.10586.36.0). It is also not supported in IE if the opener is in a different security zone.
Hm, maybe try setting something like window.open(…).opener = window; yourself in that case. Of course, if you are in different security zones (is that a Microsoft-ism for the SOP?) it never will work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.