Forum Moderators: open

Message Too Old, No Replies

JavaScript window.closed delay -- Safari bug?

         

arloleach

6:27 pm on Oct 14, 2009 (gmt 0)

10+ Year Member



Hi folks,

I've been troubleshooting an issue that happens in Safari 3 and 4 but not Firefox 3 or IE 7. I have a JavaScript function to open a window, which first checks to see if the window is already open and focuses it if it is. I'm checking the for the existence of a window object as well as looking at the window.closed property to see if the window is already (and still remains) open.

The problem is that in Safari, if I navigate to another page in the popup window, and then close it, it takes about three seconds for the window.closed property to reflect the closed state of the window. So if I immediately click a link to open the window again, my conditionals fail and Safari incorrectly thinks the window is still open.

I've set up a test case that contains only this functionality; it includes output to Safari's debug console so you can see how the script is flowing. The problem is the line that tests the value of my_window.closed. This seems like a browser bug plain and simple, but before I send feedback to Apple (for which I won't hear a reply), can anyone see something I'm missing, or an alternative to making my script work as expected?

Here's a page for testing the window opening function:


<html>

<head>

<script language="JavaScript">

function open_window() {

// first, see if the window object already exists
if (typeof(my_window) == "object") {
console.log("window object exists");

// then, see if it is still open
if (!my_window.closed) {
console.log("window still open");

// if so, focus the window and we're done
console.log("focusing existing window");
my_window.focus();
return;

} else {
console.log("window no longer open");
}

}

// if the window is not open, open it now
console.log("opening new window");
my_window = window.open("popup_1.html", "my_window_name", "width=300,height=200,location=no,menubar=no,directories=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes");

}

</script>

</head>

<body>

<p>This link should open a new window, or if it is already open, focus it:</p>

<p><a href="#" onclick="open_window();">Test</a></p>

</body>

</html>

You can use whatever you want in your popup windows, but the bug only occurs if your initial popup content contains a link so you can navigate to another document before closing the window. Here are the pages I'm using:

popup_1.html


<html>

<body>

<p>Click here to open a new document within this window:</p>

<p><a href="popup_2.html">Test</a></p>

</body>

</html>

popup_2.html


<html>

<body>

<p>Now close this window, and try to open it again.</p>

</body>

</html>

Thanks for looking!

-Arlo

Fotiman

6:55 pm on Oct 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I haven't tried it, but if it works as you describe then it sounds like a browser bug. Here is an idea for a workaround:

If you have control of the page shown in the popup, then perhaps you could run some JavaScript in that page that listens for the window.onunload event and sets some property on the window (or sets a variable in it's opener). Then you would modify your test condition to something like:

if (!my_window.closed && !my_window.closedInSafariToo)

So onunload you would do something like:

closedInSafariToo = true;

Just a thought.

arloleach

6:34 pm on Oct 21, 2009 (gmt 0)

10+ Year Member



Thanks for the reply. Isn't window.onunload unreliable, in the sense that it doesn't fire in all cases? I haven't tried using that for a long time so maybe it's more reliable in current browsers. In any case, I've submitted a bug report to Apple.