window.open Detects Popup Blocking

Detect popup blocking by checking window.open for null, then fall back to same-tab navigation and watch win.closed for the popup lifecycle.

JavascriptResilience

Code

javascriptsnippet
20 lines
// window.open returns null when the browser blocks the popup; a blocked
// origin or the user can also close it later — both paths need handling.
function startOAuthPopup(url) {
  const win = window.open(url, 'oauth', 'width=480,height=640');

  if (!win) {
    console.log('popup blocked; falling back');
    window.location.assign(url); // same-tab fallback instead of silence
    return null;
  }

  win.focus();
  const poll = setInterval(() => {
    if (win.closed) {
      clearInterval(poll);
      console.log('popup closed by user or blocker');
    }
  }, 500);
  return win;
}

Inside the code

window.open Detects Popup Blocking comes down to the function startOAuthPopup.

Watch out for

  • Nothing to vet in a supply-chain review — the file pulls in no packages.
  • window exist only in browsers — a Node runtime has no shims for them out of the box.

Using it

Signature: function startOAuthPopup(url)

The run emits 2 labeled sections — popup blocked; falling back, popup closed by user or blocker — one per step.

Key statements

const poll = setInterval(() => {
  if (win.closed) {
    clearInterval(poll);
    console.log('popup closed by user or blocker');
  }
}, 500);

Interactive Tools for This Topic

FAQ

Does window.open Detects Popup Blocking run in plain Node.js?
No — it touches `window`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.