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.
Code
// 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.
windowexist 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
DOM Event Propagation VisualizerVisualize DOM event capturing, targeting, and bubbling phases in real time. Toggle stopPropagation and stopImmediatePropagation to see how events traverse the tree.HTTP Retry & Backoff CalculatorSimulate exponential backoff retry schedules with Full, Equal, Decorrelated, or No Jitter to inspect per-attempt wait times and cumulative latency.SPDX License LookupSearch common SPDX license identifiers and see permissions, conditions, limitations, copyleft strength, and OSI or FSF approval status.
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.
Report a problem with this exampleOpens a pre-filled issue on GitHub.