Element call. Moves caret/keyboard focus into a control.
Forms need the element API. Prefer element.focus() for inputs.
Same name, different object. Do not mix them up in docs or code reviews.
Example 5 — Listen for the focus Event
React when the user returns to the tab — often more useful than calling focus().
JavaScript
window.addEventListener("focus", () => {
console.log("Window gained focus");
});
window.addEventListener("blur", () => {
console.log("Window lost focus");
});
// Switch to another tab, then come back, to see both logs.
(after you leave the tab)
Window lost focus
(after you return)
Window gained focus
How It Works
Add listeners.focus and blur on window.
Leave the tab.blur fires.
Return to the tab.focus fires.
Build features here. Resume polling, refresh data, or resume media.
Compatibility
Browser Support
Window.focus() is marked Baseline Widely available on MDN (since July 2015). Availability ≠ unrestricted focus stealing — user agents still limit when the request succeeds.
✓ Baseline · Widely available
focus()
Request to bring a window to the front across modern browsers.
99%Core tag support
ChromeYes
Full support (restricted)
EdgeYes
Full support (restricted)
FirefoxYes
Full support (restricted)
SafariYes
Full support (restricted)
OperaYes
Full support (restricted)
HTML element100% supported
Bottom line: Safe to call. Do not build critical UX that requires stealing focus from other windows.
Frequently Asked Questions
It requests that the browser bring that window (or a popup reference) to the front. MDN notes it may fail due to user settings, and the window is not guaranteed to be frontmost when the call returns.
No. MDN marks Window.focus() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
undefined. There is no success flag — you cannot tell from the return value whether the window actually came to the front.
No. window.focus() targets a browsing window or tab. element.focus() moves keyboard focus to a specific HTML control such as an input. Same name, different objects.
No. Modern browsers restrict programmatic focus for security. Calls work best after a user gesture, especially on popups your script opened with window.open().
Mostly with script-opened popups: after open(), call popup.focus() so the child window is visible. For form fields use element.focus(). To detect tab return, listen for the window focus event or use the Page Visibility API.