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 closed
property of the window.
For instance, we write
const newWin = window.open(url);
if (!newWin || newWin.closed || typeof newWin.closed == "undefined") {
//...
}
to call window.open
to open the url
.
Then we check if the window object’s closed
property is true
or undefined or if the window object is falsy.
If any of these are true, then the window is blocked from opening.
Conclusion
To detect if a browser is blocking a popup with JavaScript, we can check the closed
property of the window.