1

Let's say you have a function that is intended to open example.com:

function openExampleCom() {
    window.open('http://example.com');
}

If you try just to run it from a code or in a developer console,

openExampleCom();

a browser will likely block unwanted pop-up window instead of opening example.com. But if you run the same function in response to, let's say, click-event,

document.onclick = openExampleCom;

a browser would likely open example.com in response to your click without hesitation in a new tab/window.

My question is: what cases/events are special/"privileged" (like click-event handler) and what "privileges" do they have (like to window.open without restriction)?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Angly Cat
  • 341
  • 2
  • 7
  • http://stackoverflow.com/questions/2587677/avoid-browser-popup-blockers `window.open` is only allowed if it was invoked as a result of any user action. This is just a convention that came about as a result of the wild west of popup-heavy sites 10 or so years ago. – Prashanth Chandra Dec 06 '15 at 22:45
  • When `window.open` is called, the browser looks at the call stack to check whether it was called by a user event. So it's not so much a privilege of event handlers, as it is a feature of `window.open` – Prashanth Chandra Dec 06 '15 at 22:46
  • @PrashanthChandra, so, `window.open` is the only special case? – Angly Cat Dec 07 '15 at 02:20
  • Correction to my previous comment, this functionality is a feature of popup blockers, and are not part of any formal standard. This kind of behaviour will also vary heavily depending on which popup blocker, user's configuration and extensions, etc. https://msdn.microsoft.com/en-us/library/ms537632(v=vs.85).aspx Outdated, but shows a list of functions that were considered "obnoxious" and blocked. From what I can tell though, most of those never existed in other browsers and the ones that did (e.g. showModalDialog) have all been deprecated or removed, so only `window.open` remains – Prashanth Chandra Dec 07 '15 at 03:23

0 Answers0