Sometimes, we want to detect blocked popup in Chrome with JavaScript.
In this article, we’ll look at how to detect blocked popup in Chrome with JavaScript.
How to detect blocked popup in Chrome with JavaScript?
To detect blocked popup in Chrome with JavaScript, we can check if the popup object is defined or if its screenX
property is 0.
For instance, we write
const myPopup = window.open(url, "screenX=100");
if (!myPopup) {
alert("failed for most browsers");
} else {
myPopup.onload = () => {
setTimeout(() => {
if (myPopup.screenX === 0) {
alert("failed for chrome");
}
}, 0);
};
}
to call window.open
to open a popup that opens loads url
.
Then we check if myPopup
is defined.
If it’s not, then it didn’t open.
Otherwise, we check in the myPopup.onload
method if the screenX
property of myPopup
is 0.
If it is, then the popup also didn’t load.
Conclusion
To detect blocked popup in Chrome with JavaScript, we can check if the popup object is defined or if its screenX
property is 0.