Detecting the popup-blocker
It is often useful to be able to open a new window to present the page containing the intended content (e.g., a page containing the actual experiment as was done here) in a way that is not possible from the user openen browser window. For example, you may want to get rid of the menu and toolbar, and have the window span the entire screen. This is easily achieved using the window.open function. The problem is that most web surfers have a popup blocker activated. There's no way to circumvent this, but you would like to be able to detect wether the creation of the popup window succeeded. Then you can present a warning to the user, explain the problem, and instruct the user to click a link on the current page to try again (the popup blocker doesn't block popups that are the direct result of a click event by the user). In MSIE you can use the window.createPopup function to create a popup that won't be blocked (this type of popup is subject to the restriction that only one popup can be open at the same time), but this is not available in other browsers. Here's a simple example:
It doesn't necessarily have to be a click event, it can also be a keydown event. For example if you first click the square below and press the 'a' key, a popup window will appear. This is achieved by setting
<html>
<head>
<script>
url = 'http://www.google.com'
message = "A popup-blocker was detected!"
message += " Please click the link to try again."
popupProp = "fullscreen=yes, toolbar=no"
function openIt() {
var w = window.open(url, '_blank', popupProp)
if(!w) // popup was blocked
alert(message)
}
</script>
</head>
<body onload="openIt()">
<h1>
If the window doesn't open
<a href=# onclick="openIt()">
click here
</a>
to try again.
</h1>
</body>
</html>
If you move your mouse over the square below you can see the above script in action.
Move your mouse over this square.
If the window doesn't open
click here
to try again.
onkeydown="if(event.keyCode==65) openIt()". (See this post for making this compatible with Firefox.)
If this square has focus and the 'a'-key is pressed, a popup will open without being blocked.
