Home » Questions » Computers [ Ask a new question ]

How can I detect if a browser is blocking a popup?

How can I detect if a browser is blocking a popup?

"Occasionally, I've come across a webpage that tries to pop open a new window (for user input, or something important), but the popup blocker prevents this from happening.

What methods can the calling window use to make sure the new window launched properly?"

Asked by: Guest | Views: 236
Total answers/comments: 4
Guest [Entry]

"If you use JavaScript to open the popup, you can use something like this:

var newWin = window.open(url);

if(!newWin || newWin.closed || typeof newWin.closed=='undefined')
{
//POPUP BLOCKED
}"
Guest [Entry]

"I tried a number of the examples above, but I could not get them to work with Chrome. This simple approach seems to work with Chrome 39, Firefox 34, Safari 5.1.7, and IE 11. Here is the snippet of code from our JS library.

openPopUp: function(urlToOpen) {
var popup_window=window.open(urlToOpen,""myWindow"",""toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=yes, width=400, height=400"");
try {
popup_window.focus();
} catch (e) {
alert(""Pop-up Blocker is enabled! Please add this site to your exception list."");
}
}"
Guest [Entry]

"Update:
Popups exist from really ancient times. The initial idea was to show another content without closing the main window. As of now, there are other ways to do that: JavaScript is able to send requests for server, so popups are rarely used. But sometimes they are still handy.

In the past evil sites abused popups a lot. A bad page could open tons of popup windows with ads. So now most browsers try to block popups and protect the user.

Most browsers block popups if they are called outside of user-triggered event handlers like onclick.

If you think about it, that’s a bit tricky. If the code is directly in an onclick handler, then that’s easy. But what is the popup opens in setTimeout?

Try this code:

// open after 3 seconds
setTimeout(() => window.open('http://google.com'), 3000);

The popup opens in Chrome, but gets blocked in Firefox.

…And this works in Firefox too:

// open after 1 seconds
setTimeout(() => window.open('http://google.com'), 1000);

The difference is that Firefox treats a timeout of 2000ms or less are acceptable, but after it – removes the “trust”, assuming that now it’s “outside of the user action”. So the first one is blocked, and the second one is not.

Original answer which was current 2012:

This solution for popup blocker checking has been tested in FF (v11),
Safari (v6), Chrome (v23.0.127.95) & IE (v7 & v9). Update the
displayError function to handle the error message as you see fit.

var popupBlockerChecker = {
check: function(popup_window){
var scope = this;
if (popup_window) {
if(/chrome/.test(navigator.userAgent.toLowerCase())){
setTimeout(function () {
scope.is_popup_blocked(scope, popup_window);
},200);
}else{
popup_window.onload = function () {
scope.is_popup_blocked(scope, popup_window);
};
}
} else {
scope.displayError();
}
},
is_popup_blocked: function(scope, popup_window){
if ((popup_window.innerHeight > 0)==false){
scope.displayError();
}
},
displayError: function(){
alert(""Popup Blocker is enabled! Please add this site to your exception list."");
}
};


Usage:

var popup = window.open(""http://www.google.ca"", '_blank');
popupBlockerChecker.check(popup);

Hope this helps! :)"
Guest [Entry]

"One ""solution"" that will always work regardless of browser company or version is to simply put a warning message on the screen, somewhere close to the control that will create a pop-up, that politely warns the user that the action requires a pop-up and to please enable them for the site.

I know it's not fancy or anything, but it can't get any simpler and only requires about 5 minutes testing, then you can move on to other nightmares.

Once the user has allowed pop-ups for your site, it would also be considerate if you don't overdo the pop-ups. The last thing you want to do is annoy your visitors."