What causes a popup to be blocked?

6

I have noticed that some libraries, such as login with facebook, always use popups for user authentication, and almost always, this popup is not blocked.

Generally, when it is opened with a click event there are no locks. And when we use window.open directly on the console, for example, they are blocked.

How to determine the situations where the popup will be blocked?

Is there any way to circumvent this block? Or is there any way to "inform" that such a popup is "reliable" for opening?

    
asked by anonymous 16.11.2015 / 18:28

1 answer

7

Popups are blocked by browsers when a window.open (or equivalent) action is identified that does not appear to be an immediate user action. Older browsers could consider any function stack to be a bad signal and choose to block. In the present, the process is more sophisticated and will examine the performance of the call with a better performance.

A practical example:

<body onload="abrir(true);">
    <button id="abcd" type="button" onclick="abrir(false)">Ação Direta</button>        
</body>

<script type="text/javascript">
    function abrir(value) {
      if (value) {
        setTimeout(function () {
          window.open('http://pt.stackoverflow.com');
        }, 5000);
      } else {            
        window.open('http://pt.stackoverflow.com');
      }

    }
</script>

Even though it's the same function and using Timeout to try to cheat, Google Chrome will block a popup after 5 seconds of page loading, however it will never block the user's click.

We can simplify the function and find the same behavior:

<script type="text/javascript">
    function abrir(value) {
      window.open('http://pt.stackoverflow.com');
    }
</script>
  

What causes a popup to be blocked?

The ability of the browser to determine whether the call to the window.open function is intended by the user or not.

    
16.11.2015 / 20:05