Detect Blocked Popup in Chrome

How do I detect popup blocker in Chrome?

Finally, it success by combining different answer from Stackoverflow's member

This code worked for me & tested in IE, Chrome & Firefox

var popup = window.open(winPath,winName,winFeature,true);
setTimeout( function() {
if(!popup || popup.outerHeight === 0) {
//First Checking Condition Works For IE & Firefox
//Second Checking Condition Works For Chrome
alert("Popup Blocker is enabled! Please add this site to your exception list.");
window.location.href = 'warning.html';
} else {
//Popup Blocker Is Disabled
window.open('','_self');
window.close();
}
}, 25);

Detect blocked popup in Chrome

Well the "magical time" you speak of is probably when the popup's DOM has been loaded. Or else it might be when everything (images, outboard CSS, etc.) has been loaded. You could test this easily by adding a very large graphic to the popup (clear your cache first!). If you were using a Javascript Framework like jQuery (or something similar), you could use the ready() event (or something similar) to wait for the DOM to load before checking the window offset. The danger in this is that Safari detection works in a conflicting way: the popup's DOM will never be ready() in Safari because it'll give you a valid handle for the window you're trying to open -- whether it actually opens or not. (in fact, i believe your popup test code above won't work for safari.)

I think the best thing you can do is wrap your test in a setTimeout() and give the popup 3-5 seconds to complete loading before running the test. It's not perfect, but it should work at least 95% of the time.

Here's the code I use for cross-browser detection, without the Chrome part.

function _hasPopupBlocker(poppedWindow) {
var result = false;

try {
if (typeof poppedWindow == 'undefined') {
// Safari with popup blocker... leaves the popup window handle undefined
result = true;
}
else if (poppedWindow && poppedWindow.closed) {
// This happens if the user opens and closes the client window...
// Confusing because the handle is still available, but it's in a "closed" state.
// We're not saying that the window is not being blocked, we're just saying
// that the window has been closed before the test could be run.
result = false;
}
else if (poppedWindow && poppedWindow.test) {
// This is the actual test. The client window should be fine.
result = false;
}
else {
// Else we'll assume the window is not OK
result = true;
}

} catch (err) {
//if (console) {
// console.warn("Could not access popup window", err);
//}
}

return result;
}

What I do is run this test from the parent and wrap it in a setTimeout(), giving the child window 3-5 seconds to load. In the child window, you need to add a test function:

function test() {}

The popup blocker detector tests to see whether the "test" function exists as a member of the child window.

ADDED JUNE 15 2015:

I think the modern way to handle this would be to use window.postMessage() to have the child notify the parent that the window has been loaded. The approach is similar (child tells parent it's loaded), but the means of communication has improved. I was able to do this cross-domain from the child:

$(window).load(function() {
this.opener.postMessage({'loaded': true}, "*");
this.close();
});

The parent listens for this message using:

$(window).on('message', function(event) {     
alert(event.originalEvent.data.loaded)
});

Hope this helps.

How do I detect whether popups are blocked in chrome

Basically there's a bug in Chrome. Although it hides the popup, it still executes and you still get the window object back - so regular checks don't work.

Here's the solution that worked for me:

var popup = window.open(url);

if (popup) {
popup.onload = function () {
console.log(popup.innerHeight > 0 ? 'open' : 'blocked');
}
} else {
console.log('blocked');
}

Working example here: http://jsbin.com/uticev/3/

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

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
}

Pop up blocker API- how to check if user has it enabled

Window.open(...) returns a handle to the new window if it exists. If it doesn't have a handle to the new window, that's a pretty good indication the window was blocked.

https://developer.mozilla.org/en-US/docs/Web/API/Window/open

From: https://davidwalsh.name/popup-block-javascript

var windowName = 'userConsole'; 
var popUp = window.open('/popup-page.php', windowName, 'width=1000, height=700, left=24, top=24, scrollbars, resizable');
if (popUp == null || typeof(popUp)=='undefined') {
alert('Please disable your pop-up blocker and click the "Open" link again.');
}
else {
popUp.focus();
}

programmatically determine if the popup blocker is enabled in a browser without opening the new tab

I don't think you can detect it without using window.open, so I think the short answer to your question is: No, there isn't.

The accepted answers to this question and this question demonstrate using window.open (the first then immediately closes the window). However: If the popup is blocked, the user still receives the notification of a blocked popup (at least in Chrome, but probably others as well), which I'm guessing is why you've said you don't want to use window.open.

So again, I think the answer to your question is: No, you can't do that.

window.open' blocked by Chrome with change event

That is by design, the only time browsers don't block window.open is when you are handling a click event.

My suggestion is to provide a link that changes when users select from the dropdown.

I advise against opening a popup because users don't expect a popup when you select from a drop down, that is why popup blockers don't typically allow this. Even if you find something that works in a browser (https://jsfiddle.net/yyfe0824/5/ in Firefox), it could break in the future.



Related Topics



Leave a reply



Submit