Check Whether Browser Tab Is Focused in JavaScript
- 1 minute read
The following snippet returns true
if the current browser tab is focused, false
if the current browser tab is not focused, and null
if
document.hidden is not supported by the user’s browser.
const isFocused = () => typeof document.hidden !== `undefined` ? !document.hidden : null
To use the snippet, simply run the isFocused
function:
isFocused();
While isFocused
will return null
if the user’s browser doesn’t support document.hidden
,
most modern browsers support it.
Conclusion
Anyway, that’s all! I hope you found this useful.
Thanks for reading.