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.