JavaScript Answers

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

Spread the love

Detecting whether a popup has been blocked by the browser using JavaScript can be tricky because most modern browsers do not provide a direct API for such detection due to privacy and security concerns.

However, you can use a workaround to infer whether the popup has been blocked or not.

One common approach is to open the popup and then check if it was successfully opened or not.

If it wasn’t, it’s likely that the browser blocked it.

Here’s how you can do it:

function openPopup(url, name, width, height) {
    // Open the popup
    var popup =, name, 'width=' + width + ',height=' + height);

    // Check if the popup was successfully opened
    if (popup) {
        // Popup was opened successfully
        console.log('Popup opened successfully');
    } else {
        // Popup was blocked
        console.log('Popup was blocked by the browser');

In this code, the function attempts to open a popup window with the specified URL, name, width, and height.

If the browser allows the popup, it returns a reference to the newly opened window. Otherwise, it returns null.

By checking the value returned by, we can infer whether the popup was blocked or not.

However, note that this method has limitations and may not always accurately detect if the popup was blocked.

Some browsers, for example, may silently block popups without returning null from

Additionally, some browsers may have popup blockers disabled, or the user may have configured exceptions for certain sites.

So, while this method can provide some indication, it’s not foolproof, and it’s important to consider alternative approaches if accurate detection is crucial for your application.

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 *