JavaScript Document visibilityState Property

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance property

What You’ll Learn

Document.visibilityState is a read-only instance property that returns a string describing whether the page is "visible" or "hidden". Learn the Page Visibility API, MDN’s visibilitychange event, how it relates to document.hidden, and five examples with try-it labs.

01

Kind

Read-only

02

Returns

string

03

visible

Foreground tab

04

hidden

Background tab

05

Event

visibilitychange

06

Status

Baseline widely

Introduction

Users often leave a tab open but switch away. Your page may still run JavaScript in the background—burning battery, playing audio, or polling a server. The Page Visibility API lets scripts know when the document is actually visible.

MDN: the visibilityState read-only property returns the visibility of the document. It can be used to check whether the document is in the background or in a minimized window, or is otherwise not visible to the user. When the value changes, the visibilitychange event is sent to the Document.

💡
Two states (MDN)

"visible" — page content may be at least partially visible (foreground tab of a non-minimized window). "hidden" — page content is not visible (background tab, minimized window, or OS screen lock).

Related Document tutorials: hidden, prerendering, readyState, Document constructor.

Understanding Document.visibilityState

A read-only string instance property from the Page Visibility API.

  • "visible" — page may be at least partially visible (MDN).
  • "hidden" — page content is not visible to the user (MDN).
  • Eventvisibilitychange fires when the value changes (MDN).
  • Alternativedocument.hidden boolean (MDN).
  • Status — Baseline Widely available (since July 2015, MDN).

📝 Syntax

JavaScript
document.visibilityState

Value

A string with one of: "visible" or "hidden" (MDN).

MDN: listen for changes

JavaScript
document.addEventListener("visibilitychange", () => {
  console.log(document.visibilityState);
  // Modify behavior…
});

⚡ Quick Reference

GoalCode / note
Read statedocument.visibilityState
Is visible?document.visibilityState === "visible"
Is hidden?document.visibilityState === "hidden"
Listen for changesdocument.addEventListener("visibilitychange", …)
Boolean shortcutdocument.hidden
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about document.visibilityState.

Type
string

Read-only

visible
"visible"

Active tab

hidden
"hidden"

Background

Status
baseline

Since 2015

📋 visibilityState vs document.hidden

document.visibilityStatedocument.hidden
Typestringboolean
Visible tab"visible"false
Hidden tab"hidden"true
MDN rolePrimary string stateAlternative boolean (MDN)

Examples Gallery

Examples follow MDN Document: visibilityState. Switch tabs in the try-it editor to see the state change.

📚 Getting Started

Read the property and listen for MDN’s event.

Example 1 — Read document.visibilityState

On the active tab, the value is usually "visible".

JavaScript
console.log(document.visibilityState);
console.log("visible?", document.visibilityState === "visible");
Try It Yourself

How It Works

This is a snapshot. Use visibilitychange to react when the string updates.

Example 2 — MDN: visibilitychange Listener

Log document.visibilityState whenever visibility changes (MDN).

JavaScript
document.addEventListener("visibilitychange", () => {
  console.log(document.visibilityState);
  // Modify behavior…
});
Try It Yourself

How It Works

MDN’s core pattern: one event, read the string, update behavior accordingly.

📈 Branch, Compare & Live UI

Practical patterns using the visibility string.

Example 3 — Branch on "visible" vs "hidden"

Pause work when hidden; resume when visible.

JavaScript
function onVisibilityChange() {
  if (document.visibilityState === "hidden") {
    console.log("Page hidden — pause timers, media, polling");
  } else {
    console.log("Page visible — resume activity");
  }
}

document.addEventListener("visibilitychange", onVisibilityChange);
onVisibilityChange();
Try It Yourself

How It Works

String comparison is explicit and readable compared to inverting a boolean.

Example 4 — Compare with document.hidden

MDN lists hidden as an alternative way to detect a hidden page.

JavaScript
function logVisibility() {
  console.log({
    state: document.visibilityState,
    hidden: document.hidden
  });
}

document.addEventListener("visibilitychange", logVisibility);
logVisibility();
Try It Yourself

How It Works

hidden === true corresponds to visibilityState === "hidden" on typical pages.

Example 5 — Live Status Badge in the Page

Update UI text whenever the visibility string changes.

JavaScript
const badge = document.getElementById("status");

function renderState() {
  badge.textContent = document.visibilityState;
  badge.dataset.state = document.visibilityState;
}

document.addEventListener("visibilitychange", renderState);
renderState();
Try It Yourself

How It Works

Great for debug panels and teaching demos—users see the string update live.

🚀 Common Use Cases

  • Media playback — pause video/audio when state is "hidden".
  • Animations — pause requestAnimationFrame loops in background tabs.
  • Analytics — measure engaged time only while "visible".
  • Live dashboards — stop or slow polling when hidden.
  • Games — pause gameplay when the user switches away.
  • Autosave — flush drafts when visibility becomes "hidden".

🧠 How document.visibilityState Works

1

User changes focus

Switch tab, minimize window, or lock screen (MDN).

Browser
2

State string updates

visibilityState becomes "visible" or "hidden".

Property
3

visibilitychange fires

Your listener runs on the Document (MDN).

Event
4

Script adapts behavior

Pause media, stop timers, or save state when hidden.

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • MDN documents two values: visible and hidden.
  • document.hidden is the boolean alternative (MDN).
  • blur/focus on window are not a substitute for tab visibility.
  • Related: hidden, prerendering, URL.

Browser Support

Document.visibilityState is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

Document.visibilityState

Read-only string — visible or hidden Page Visibility state.

Widely Available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported (legacy)
Legacy support
Document.visibilityState Baseline support

Bottom line: Use document.visibilityState with visibilitychange to pause media, timers, and polling when the tab is hidden.

Conclusion

Document.visibilityState tells you whether the page is "visible" or "hidden". Listen for visibilitychange, read the string in your handler, and pause expensive work when the user is not looking at the tab. For a quick boolean check, MDN also documents document.hidden.

Continue with xmlEncoding, ownerDocument, hidden, URL, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use visibilitychange instead of polling
  • Compare to "visible" / "hidden" explicitly
  • Pause media and timers when state is "hidden"
  • Resume gracefully when returning to "visible"
  • Pair with document.hidden when a boolean is enough

❌ Don’t

  • Assign to document.visibilityState (read-only)
  • Rely on window blur alone for tab visibility
  • Keep heavy polling running in hidden tabs
  • Assume visibility never changes on mobile
  • Forget to remove listeners in short-lived components

Key Takeaways

Knowledge Unlocked

Five things to remember about document.visibilityState

Page Visibility string — visible or hidden.

5
Core concepts
02

visible

Foreground

MDN
🚫03

hidden

Background

MDN
🔔04

Event

visibilitychange

Listen
📈05

Status

Baseline

2015+

❓ Frequently Asked Questions

A read-only string describing document visibility. MDN documents two values: visible (page may be at least partially visible) and hidden (page content is not visible to the user).
No. MDN marks Document.visibilityState as Baseline Widely available (since July 2015). It is part of the standard Page Visibility API.
document.hidden is a boolean shortcut. document.visibilityState returns the exact string state. MDN notes hidden as an alternative way to determine whether the page is hidden.
MDN: when the document is a background tab, part of a minimized window, or the OS screen lock is active — the page content is not visible to the user.
The visibilitychange event on document. Read document.visibilityState inside the handler to react (MDN example).
No. It is read-only. You observe visibility; you cannot force the page hidden from script.
Did you know?

MDN states that when visibilityState changes, the visibilitychange event is sent to the Document. That means one listener can pause video, stop polling, and update analytics—all from a single event instead of checking visibility on every timer tick.

Next: xmlEncoding

Learn the deprecated XML declaration encoding property and why to use characterSet instead.

xmlEncoding →

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.

6 people found this page helpful