Sometimes, we want to detect if a browser is blocking a popup with JavaScript.
In this article, we’ll look at how to detect if a browser is blocking a popup with JavaScript.
How to detect if a browser is blocking a popup with JavaScript?
To detect if a browser is blocking a popup with JavaScript, we can check the popup window’s state.
For instance, we write
const newWin = window.open(url);
if (!newWin || newWin.closed || typeof newWin.closed == "undefined") {
//...
}
to call window.openn
to open a popup window with the url
.
Then we check for the closed
property or whether newWin
or newWin.closed
is undefined to check if it’s been blocked.
Conclusion
To detect if a browser is blocking a popup with JavaScript, we can check the popup window’s state.