If I have created a popup window using newWindow = window.open(url, name, dimensions), how can I check whether the window is closed in a way that will work on mobile browsers?
I tried using if(newWindow.closed), and this works in Chrome on a PC and works on iPhone. However, on the Android phones I have tested on, this doesn’t work; the popup closes but newWindow.closed is not true. What test should I use instead that will work on any platform to check if a popup is closed?
Extra info:
I used jsconsole to log newWindow (with console.log(newWindow)) before and after the popup was closed when visiting my page on an Android phone. While the popup window is open, it shows up in the console as [object DOMWindow], and once the window is closed console.log(newWindow) just prints a blank line to the jsconsole. Note that printing a blank line is distinct from how jsconsole displays null, undefined or false, which show up exactly as I just typed them. Furthermore, trying if(newWindow.closed || !newWindow) works no better than just if(newWindow.closed) did; it seems that whatever kind of object newWindow becomes after the popup is closed, it is still truthy.
Checking
if(newWindow.closed != false)instead ofif(newWindow.closed)solved my problem and works on all platforms I have tested on. The cause of the issue appears to be that on some Android phones, the value ofnewWindow.closedends up beingundefinedinstead oftruewhen the popup window is closed.newWindow.closedis reliably set tofalsewhile the window is open, though, whatever platform you’re on.Some relevant discussion that potentially explains the reasons behind this bug can be found here (although I can’t confirm its veracity):
http://code.google.com/p/android/issues/detail?id=21061