0

--- EDIT.

I don't get it; yesterday it seemed to be ignored; now it seems to be working. Even within loop and called via setTimeout(). Currently I seem to be having difficulties replicating yesterday's behaviour... What is going on?


--- 2nd EDIT.

First idea how to "fix" the replication of the issue: weirdly, whether or not this works seems to be dependent on the current URL! E.g. works from SE-sites, but not from, say, http://www.asdf.com/. How so?


Whereas setTimeout() works here:

setTimeout(function(){ alert("Hello"); }, 3000);

and window.open() works here:

window.open("https://www.bbc.com","_self");

and even the combination of the two works here:

setTimeout(function(){ window.open("https://www.bbc.com","_self") }, 3000);

suddently and unexpectedly the combination of the two is - silently - ignored in a loop:

i=0;
while(i < 100)
{
    setTimeout(function(){ window.open("https://www.bbc.com","_self") }, 3000);
    i++
}

Why?


tldr;

This question seems to have come up close to a million times already but not yet (as far as I could tell / search) with a succinct Q&A format; e.g.

Settimeout() javascript function is ignored

Simulating JavaScript's setTimeout() method “from first principles”

nutty about natty
  • 1,267
  • 2
  • 10
  • 17
  • 1
    Popup blocker?? `setTimeout(function(){ var w = window.open("https://www.bbc.com","_self"); if (!w) alert("failed") }, 3000);` Any message in the console at all? – mplungjan May 14 '18 at 08:34
  • @mplungjan if it's in the console or somewhere, then I'd need to correct/update the Q&A accordingly (not "silent"). – nutty about natty May 14 '18 at 08:38
  • Browsers will often ignore `window.open()` if it's invoked in a piece of code that's not triggered by a user interaction event (e.g. an `onclick` listener) – Máté Safranka May 14 '18 at 08:38
  • @MátéSafranka yes, that's what I realized (and wanted to share). – nutty about natty May 14 '18 at 08:38
  • @nuttyaboutnatty - that is assuming you KNEW about the console. I did not assume since many questions at SO says one thing and then when asked, more details appear, so it is worth asking. For example some times you need to "preserve log" to see all messages – mplungjan May 14 '18 at 08:46
  • I don't get it; yesterday it seemed to be ignored; now it works?!?! even within loop and called via setTimeout(). What's going on? – nutty about natty May 14 '18 at 09:01

1 Answers1

2

Popup blockers in most popular browsers will only allow a new window to be opened if it is opened as a result of code running from a direct user action such as a click. Because a setTimeout() happens some time in the future, is not considered the direct result of a user action so attempts to open windows from setTimeout() are likely blocked by the popup blocker.

In essence, trying to fire window.open from within setTimeout() leaves the browser to "think" it's a popup which deserves (silent) blocking. -- If, in contrast, window.open is fired on it's own, the browser seems to treat it like a "user click", that is, not as spam to be blocked.

nutty about natty
  • 1,267
  • 2
  • 10
  • 17