Categories:
Partners

Determining whether a browser window is open or not

This question must have popped up several times in the CodingForums, so I thought it's time I formally addressed it. Which question is that? Well, many people wanted to know if there was a way to determine whether a window opened through JavaScript (using window.open()) is still open at a particular moment in the future. "Why would it not be?" you ask. Simple. The surfer might have decided to close it, which can cause problems if your script doesn't know about it, and decides to reference and manipulate it. Specifically, a JavaScript error message will be generated. So, without further adieu, let's learn how to determine whether a browser window is open or not after it's been opened using JavaScript. It's actually a very simple process.

The closed property of the window object

The closed property tells you whether a window opened using window.open() is still open or not. You see, once a window is opened (using JavaScript), it's closed property is immediately initialized, with a value of false. If and when this newly opened window is closed, it's "closed" property continues to exist in your computer's memory,  but has a value of true instead. Essentially, the closed property of the window object always reflects the current state of the opened window in terms of whether it's still open or not.

Ok, let's actually do an example. Suppose I decide to open a new window using the below code:

<script type="text/javascript">
win2=window.open("test.htm")
</script>

Now, let's say I've also created a script somewhere following that that reloads the contents of this newly opened window every 10 seconds (perhaps to serve up a new ad banner periodically, for example). This code might look something like this:

<script type="text/javascript">
function reloadwin2(){
win2.location.reload()
setTimeout("reloadwin2()",10000)
}
reloadwin2()
</script>

The above code will do what it's suppose to do- as long as "win2" doesn't unexpectedly get shut down by your annoyed surfer. If it is, a nasty JavaScript error (actually, many) will pop up instead, stating that "win2" does not exist. How can we remedy this so that our "reload window" code addresses this possibility? You've guessed it- by using the window.closed property. Take a look at the modified code below:

<script type="text/javascript">
function reloadwin2(){
if (!win2.closed)
win2.location.reload()
else
return
setTimeout("reloadwin2()",10000)
}
reloadwin2()
</script>

By using the closed property, I can determine whether win2 is still open before reloading it, thus avoiding a potential JavaScript disaster.

You can also use the "closed" property on the window.opener object to determine whether the window that opened the window in question is still open (are you still with me?). For example:

<script type="text/javascript">
if (window.opener.closed)
alert("You killed my boss!")
else
alert("My boss is still alive!")
</script>

Pretty neat, uh?