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

John Jerrby picture John Jerrby · Sep 11, 2016 · Viewed 12.7k times · Source

I need to know when the user clicks on the button that triggers window.open if there is stable API/way to know beforehand if the user actively has a pop-up blocker?

In some cases the user doesn't know/pay attention that they have pop-up blocker (that block the new window). I would like to inform them by some dialog/or something to authorize a new window by clicking on allow.

Answer

xaxxon picture xaxxon · Sep 16, 2016

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();
}