How to check if your popup was blocked using JavaScript

17 Jan 2010 Ferado

When I researched how to get pop ups to not be blocked using flash, in that research, I found out how to check and see if a popup was blocked or not. It’s such a simple solution I don’t know why I haven’t consciously noticed it before. I’ve created many popups in the past, just never thought about how I would check if a popup was blocked.

The Solution

Using JavaScript, put something like this in your code:
[cc lang=”javascript”]
// this is sample code so replace
// wurl = the url you want opened
// wname = name given to the window so you can reference it again programmically
// wfeatures = optionally you can give it scroll bars, make it resizeable, etc.
var win = window.open(wurl, wname, wfeatures);
if(win == null || typeof(win) == “undefined” || win.location.href == ‘about:blank’) {
alert(“Please enabled popups for this site to continue.”);
}
[/cc]

That’s it! That’s all you need to do to test if someone has blocked your popup. Now you need to decide if it’s worth the hassle of working with popups and how to handle when a popup is blocked.

The three simple ways around using popups are:

  1. Use target=”_blank” on your anchor tag
  2. Use a modal popup, which is just a layer within the DOM
  3. Use inline code that slides down, up or sideways

Remember that you should never automatically open a new window for a user. That’s just rude and unethical these days. Use something inline instead. Only open a new window after a “click” event has been fired. Automatic popups are evil.

5 comments on “How to check if your popup was blocked using JavaScript

  1. Michael Barton

    @PV, I’m not sure what you’re asking, and I know I’ve been M.I.A. for a while. In general, in javascript, you check the url like so: window.location.href. In our example, it would be win.location.href.