JavaScript Window focus() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Method

Definition

window.focus() makes a request to bring that window to the front (MDN). It returns undefined.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call window.focus. Start with this line.

JavaScript
focus() / window.focus()
CallWhat it does
focus() / window.focus()Primary signature.
window.focus(...)Call with the arguments your case needs.

Built into the browser. Call window.focus(...) (or the global focus(...)) — no import required.

Library declarations
JavaScript
focus() / window.focus()

Parameters

This member has no parameters beyond those shown in the signature.

Return Value

Return ValueDescription
*Always undefined

JavaScript Examples

Example 1 — Basic focus()

Request focus for the current window from a button click.

JavaScript
window.focus();
// same as: focus();
console.log("focus() called");
console.log("hasFocus:", document.hasFocus());
Try It Yourself

How It Works

  1. Call window.focus(). No arguments.
  2. Browser may honor it. Often a no-visible-change if already active.
  3. Log a note. Confirms the script continued.
  4. Optional check. document.hasFocus() reports whether this document is focused.

Example 2 — Return Value

focus() always returns undefined.

JavaScript
const result = window.focus();
console.log(result);        // undefined
console.log(typeof result); // "undefined"
Try It Yourself

How It Works

  1. Store the call. Capture the return value.
  2. Log it. Always undefined.
  3. Check the type. typeof prints "undefined".
  4. No success flag. You cannot detect a focus win from the return value.

Example 3 — Focus a Popup

Open a child window, then request focus on that reference.

JavaScript
const popup = window.open("", "demo", "width=320,height=200");
if (popup) {
  popup.document.write("

Popup demo

"); popup.focus(); // request — may be blocked console.log("Asked popup to focus"); } else { console.log("Popup blocked"); }
Try It Yourself

How It Works

  1. Open from a click. Popups need a user gesture.
  2. Keep the reference. popup is a Window.
  3. Call popup.focus(). Asks the browser to raise that window.
  4. Handle blockers. If open returns null, show a message instead.

Example 4 — vs element.focus()

Window focus and element focus are different APIs.

JavaScript
window.focus();                 // browsing window / tab
document.getElementById("name").focus(); // keyboard focus on a control
Try It Yourself

How It Works

  1. Window call. Targets the tab / browsing context.
  2. Element call. Moves caret/keyboard focus into a control.
  3. Forms need the element API. Prefer element.focus() for inputs.
  4. 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.
Try It Yourself

How It Works

  1. Add listeners. focus and blur on window.
  2. Leave the tab. blur fires.
  3. Return to the tab. focus fires.
  4. Build features here. Resume polling, refresh data, or resume media.

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
Chrome Yes
Full support (restricted)
Edge Yes
Full support (restricted)
Firefox Yes
Full support (restricted)
Safari Yes
Full support (restricted)
Opera Yes
Full support (restricted)
HTML element 100% 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.

Next: getComputedStyle()

Read final computed CSS values on DOM elements.

getComputedStyle() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful