I recently needed to tackle the problem of detecting pop-up blockers for my job. While I found some useful information through Google search, many of the code examples did not work in all scenarios and were focused on opening an arbitrary window just to close it to then see if the pop up window was blocked. In real world scenarios, you often want to detect that a newly opened window was blocked — without closing it — so that you can then take some useful action on the blocked event, like say using location.href to redirect to the appropriate URL.
I thought it would be useful to others to post my version of pop up blocking here. I have tested this code on Firefox and Internet Explorer 6 on Windows. It works in IE with Yahoo!, Google, MSN, or AOL toolbar’s pop-up blockers.
<script type="text/javascript"> function checkOpenWindow (openWin, keepFocus) { var exists = false; if (openWin && !openWin.closed) { exists = true; try { openWin.focus(); } catch (e) { keepFocus = false; exists = false; } if (!keepfocus) { window.focus(); } } if (!exists) { alert("The window you attempted to open was blocked"); // do something here } else { alert("Your window successfully opened"); // generally do nothing } } var popupWin = window.open('http://www.thediatribe.net/', 'popupName', 'width=500,height=500,toolbars=no'); checkOpenWindow(popupWin); </script>
An explanation:
- The popupWin variable provides a reference to the window opened through window.open. Storing the return value of window.open is the only method JavaScript provides for accessing the opened window. The return value of window.open may be null.
- The checkOpenWindow function contains the logic to determine if the popup window was successfully opened. I have included a keepFocus parameter here (described further down). You could easily extend the function to provide callbacks for handling when the window exists or does not exist.
- The most basic check is to determine if the openWin parameter is not empty.
- The window object has a closed property that will return true if the window has been closed.
-
Some popup blockers, especially the toolbar ones, close the window in such a way that the window’s closed property returns false even though the window has been blocked. As a final check, we try to call the focus method on the openWin. If that fails, then the window was blocked.
window.focus() is called to revert the focus to the parent window. If the keepFocus parameter is set to true, the focus will not be reverted. However, if the opened window does not exist the focus is always reverted. - I have seen many code samples online that use a setTimeout to determine if the window has been blocked for Yahoo! toolbar. In that approach, you do not need to focus the opened window, however, I have found that the only way to properly detect popup blocking from the AOL toolbar is to attempt to focus the opened window.

You should still use a small timeout to allow slower machines time to launch the window. You may get some false negatives otherwise.
Does this code still work in 2009?