Call with no args. MDN: hasFocus() takes no parameters.
Get a boolean.true if the document/active element has focus.
Type check. Result is always a boolean, never a string.
Context matters. In a focused try-it iframe you often see true.
Example 2 — Focus Status Message
MDN-style branch: show different text when the document has or lacks focus.
JavaScript
function checkDocumentFocus() {
if (document.hasFocus()) {
return "This document has focus.";
}
return "This document does not have focus.";
}
console.log(checkDocumentFocus());
Focus first.hasFocus() = keyboard/UI focus for this document.
Visibility second.visibilityState / hidden = is the page visible?
They can differ. A visible page can still lack focus (another window on top).
Pick the right API. Pause sound on hidden tabs; pause typing shortcuts when unfocused.
Compatibility
Browser Support
Document.hasFocus() is Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Document.hasFocus()
Boolean document-focus check across all major browsers.
BaselineWidely available
Google ChromeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
Microsoft EdgeSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported (legacy)
Yes
hasFocus()Wide
Bottom line: Safe for focus-aware UI. Pair with activeElement for which node is active, and with Page Visibility for background tabs. See the MDN Document.hasFocus() reference.
Frequently Asked Questions
MDN: Document.hasFocus() returns a boolean indicating whether the document or any element inside the document has focus. It can determine whether the active element in a document has focus.
No. MDN marks Document.hasFocus() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
false if the active element in the document has no focus; true if the active element in the document has focus (MDN).
No. MDN: an element with focus is always the active element, but an active element does not necessarily have focus — for example in a popup that is not the foreground.
hasFocus() answers whether this document has keyboard/UI focus. Page Visibility (visibilityState / hidden) answers whether the page is visible (for example, not in a background tab). They are related but not identical.