Categories
JavaScript Answers

How to detect if a browser is blocking a popup with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *