document.hasFocus() is an instance method that returns a boolean: does this document (or something inside it) currently have focus? (see MDN Document: hasFocus()). Learn how it relates to activeElement, window switching, and five try-it labs.
01
Kind
Instance method
02
Args
None
03
Returns
boolean
04
true
has focus
05
false
no focus
06
Status
Baseline
Fundamentals
Introduction
When a user clicks into your page, tabs away, or opens another window, keyboard focus moves. document.hasFocus() answers a simple question: does this document have focus right now?
MDN: the method returns a boolean indicating whether the document or any element inside the document has focus. You can use it to determine whether the active element in a document has focus.
💡
Think: “is this tab/window the active one for typing?”
1) Call document.hasFocus() 2) Get true or false 3) Update UI, pause work, or resume when focus returns 4) Remember: focus ≠ visibility ≠ selection
⚠️
Active element vs focus (MDN)
When viewing a document, an element with focus is always the active element — but an active element does not necessarily have focus. Example from MDN: an active element inside a popup that is not the foreground does not have focus.
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 check for whether the document or an element inside it currently has focus.
BaselineWidely available
Google ChromeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
Microsoft EdgeSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported (legacy)
Yes
hasFocus()Wide
Bottom line: Use hasFocus to know if this document can receive keyboard focus. Pair with activeElement and Page Visibility when you need richer lifecycle checks.
Wrap Up
Conclusion
document.hasFocus() is a tiny, useful boolean API: it tells you whether this document currently has focus. Combine it with activeElement and visibility checks when your app needs a full picture of user attention.
Use hasFocus() for a quick boolean focus check (MDN)
Re-check on window focus/blur (or poll briefly while testing)
Remember active ≠ focused in background popups (MDN)
Pair with visibilityState for tab lifecycle logic
Keep UI feedback clear when focus is lost
❌ Don’t
Confuse focus with text selection
Assume activeElement always means the page has focus (MDN)
Treat visibility alone as a focus substitute
Poll forever at high frequency in production without need
Forget to test by switching windows / tabs
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about hasFocus()
A boolean check for document focus.
5
Core concepts
📝01
Returns
boolean
MDN
🔄02
Args
none
MDN
🎯03
≠ active
can differ
MDN
⚡04
Related
visibility
API
🛡05
Status
Baseline
2015
❓ 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 answers whether the page is visible (for example, not in a background tab). They are related but not identical.
No. MDN: hasFocus() takes no parameters.
Did you know?
MDN’s classic demo changes the page background from white to gray whenever document.hasFocus() flips — a visual cue that is easy for beginners to understand while switching windows.