1. Home
  2. Computing & Technology
  3. JavaScript

Can JavaScript Close This Window?

How to Tell if the Current Window (or Tab) was Opened From JavaScript

One of the rules on how JavaScript interacts with the web browser window is that JavaScript is only allowed to close a browser window if that window was opened by JavaScript. If the particular browser window was already opened by your visitor before they can to your site then you are not allowed to close their browser window.

The only browser that disregards this rule and will allow a browser to close a window that it didn't open is Internet Explorer. Even that browser doesn't just allow your JavaScript to close web browsers indiscriminantly though. If the browser window you are trying to close in IE is not one that your JavaScript opened then your visitor will see a confirmation dialog asking them to confirm if they will allow your script to close their browser. If they deny the request then their browser window stays open exactly the same as happens with all browsers except for Internet Explorer except that the other browsers don't even ask, they just assume that web pages shouldn't be trying to close browser windows that they didn't open.

One potential problem with this is that you may not know if the current page is in a browser window that your site opened using JavaScript or whether it is in a window that was already open. You may have several differnt ways that the current page can be reached some of which involve opening a new browser window and some of which don't. It is only polite to close the extra window that you opened when you are finished with it but if the path they followed to get to this page didn't involve opening a new window then there isn't a new window to close. If it weren't for the way IE behaves then we could just try closing the current window anyway. If the window was opened from our JavaScript it would close while if it were not a window that we opened then the request would be ignored. Unforunately the way IE behaves we would get that unwanted confirm box if we did that.

So how do we tell if our web page is in a popup window that we opened? Well it is still not quite that simple since it is possible for someone to configure their browser so that all JavaScript window.open requests open a new tab in their existing browser window instead of opening a separate window. That doesn't overly complicate things though because the new tab is treated exactly as a new window would be and a request by the page to close the window would in fact only close the tab in that instance and not the entire browser window.

The answer to how to detect if the current window or tab was opened by JavaScript is quite simple because when JavaScript opens a new window the javaScript in that window can access the web page that opened it using opener which is an object that points to the window that opened the current one if the current window was opened by JavaScript and which is null or undefined if the current window wasn't opened from JavaScript (depending on the browser being used).

opener only provides access to the content of the opening page while that page is still loaded in that window. Changing the page in that window to one from a different domain will mean that opener can no longer access the content of the page in that window, it will not however reset the value of opener back to null or undefined and so testing opener will still allow us to tell if the current window was opened from JavaScript or not.

To see how testing this field works let's open a second copy of this web page in a new window (or tab if you have your browser configured that way. Simply select to open a copy of the page and we now have two copies of the page, one in our original browser window and one opened from JavaScript. You can see the difference between the two by trying to close each of the two windows. The one opened from JavaScript will have closed if you selected the close link while the original will not. You can also easily tell which is which because this page is set to give an alert advising you that it has been opened in a new window and will not show the alert when the page loads into a window not opened by the current site. This can easily be confirmed by refreshing the page so as to redisplay the alert.

So what does the code look like that you need in order to tell if JavaScript opened this window? Well here's my alert code from the top of the page:

if (opener) alert('this page is in a window ...');

Here's the code from my conditional close link that only closes the page if it was opened from JavaScript and which doesn't pop up the unwanted confirm dialog from Internet Explorer.

if (opener) self.close();
Explore JavaScript
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.