1. Technology
Send to a Friend via Email

Popup Blocked

clr gif
Join the Discussion

Questions? Comments?

All web browsers these days incorporate a popup blocker. These blockers are intended to stop popup windows from appearing except when they are requested by the person using the browser.

Whether your particular popup window will be blocked depends on how you have coded the call to open the popup and also on the security settings in the browser itself. While you can be fairly confident that popup windows that open themselves will be blocked in most browsers some people have their browser set to block all popups no matter how called.

So how can we tell whether our popup window has successfully opened or not? Well exactly what gets defined for a blocked popup depends on the browser. Firefox reports that the popup itself is undefined while Internet Explorer and Opera define the popup but don't set up any content within it. So after declaring our window.open we first need to test if the reference to that window exists. If it doesn't then the popup has been blocked. If it does then either the popup has displayed or the browser is one that defines the popup reference. To resolve this we can test for one of the attributes (eg. top) that the window will have if it has opened. If that exists then the popup has opened and if it doesn't then the window has been blocked. The following code shows how you can do this (I have omitted the parameters for the open as you can use whatever is appropriate for your popup):

var newWin = window.open(...);
if (newWin && newWin.top) {
// popup has opened
} else {
// popup has been blocked
}

Alternatively if you just need to have extra code to run if the window fails to open you can test if (!newWin || !newWin.top).

©2014 About.com. All rights reserved.