JavaScript Document hidden Property

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

What You’ll Learn

Document.hidden is a read-only instance property that returns true when the page is not visible to the user (background tab, minimized window, etc.). Learn the Page Visibility API, MDN’s visibilitychange example, pausing media, and five examples with try-it labs.

01

Kind

Read-only

02

Returns

boolean

03

true

Tab hidden

04

Event

visibilitychange

05

vs

visibilityState

06

Status

Baseline widely

Introduction

When a user switches tabs or minimizes the browser, your page may still run JavaScript—but it is no longer visible. document.hidden tells you that in one boolean value.

MDN: the read-only property returns whether the page is considered hidden. Use it to check whether the document is in the background, minimized, or otherwise not visible to the user. It is part of the Page Visibility API.

💡
Why it matters

Pause videos, stop animations, reduce polling, and save battery when document.hidden is true. Resume when the user returns and it becomes false.

Related Document tutorials: head, body, activeElement, Document constructor.

Understanding Document.hidden

A read-only instance property on Document. Its value updates as the user changes tab or window visibility.

  • Valuetrue if the page is hidden; false if visible (MDN).
  • Event — listen for visibilitychange on document (MDN).
  • Alternativedocument.visibilityState returns "visible", "hidden", or "prerender" (MDN).
  • Not settable — you observe visibility; scripts cannot assign a hidden state.
  • Common patternif (document.hidden) pause(); else play();

📝 Syntax

JavaScript
document.hidden

Value

A boolean: true when the page is hidden, false when visible (MDN).

MDN example

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

⚡ Quick Reference

GoalCode / note
Is tab hidden?document.hidden
Is tab visible?document.hidden === false
React to changesdocument.addEventListener("visibilitychange", ...)
More detaildocument.visibilityState
Pause when hiddenif (document.hidden) video.pause()
MDN statusBaseline Widely available (since Jul 2015)

🔍 At a Glance

Four facts about document.hidden.

Type
boolean

Read-only

true
hidden

Background

false
visible

Active tab

Status
baseline

Widely available

📋 hidden vs visibilityState

document.hiddendocument.visibilityState
Typebooleanstring
Easiest check?YesCompare to "visible"
Prerender?Usually true"prerender" explicitly
MDNThis propertyAlternative (MDN)

Examples Gallery

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

📚 Getting Started

Read the property and listen for MDN’s event.

Example 1 — Read document.hidden

On the active tab, the value is usually false.

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

How It Works

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

Example 2 — MDN: visibilitychange Listener

Log document.hidden whenever visibility changes.

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

How It Works

MDN’s core pattern: one event, one boolean, update behavior accordingly.

📈 Pause, Compare & Save Resources

Real-world patterns for media, state strings, and timers.

Example 3 — Pause Video When Hidden

Resume playback when the user returns to the tab.

JavaScript
const video = document.querySelector("video");

document.addEventListener("visibilitychange", () => {
  if (document.hidden) {
    video.pause();
  } else {
    video.play().catch(() => {});
  }
});
Try It Yourself

How It Works

Common UX: don’t keep audio/video running in background tabs without user intent.

Example 4 — Log hidden and visibilityState

Compare the boolean with MDN’s string alternative.

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

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

How It Works

MDN recommends visibilityState when you need the exact state string (for example prerender).

Example 5 — Stop Polling When Hidden

Save CPU and network by pausing intervals in background tabs.

JavaScript
let timerId = setInterval(() => console.log("poll"), 2000);

document.addEventListener("visibilitychange", () => {
  if (document.hidden) {
    clearInterval(timerId);
    timerId = null;
    console.log("polling stopped");
  } else if (!timerId) {
    timerId = setInterval(() => console.log("poll"), 2000);
    console.log("polling resumed");
  }
});
Try It Yourself

How It Works

Dashboards and live feeds often throttle or stop updates when document.hidden is true.

🚀 Common Use Cases

  • Video / audio — pause when the tab is hidden.
  • Animations — pause requestAnimationFrame loops in background.
  • Analytics — measure time-on-page only while visible.
  • Live data — stop or slow polling when hidden.
  • Games — pause gameplay when the user switches away.
  • Autosave — flush drafts when visibility becomes hidden.

🧠 How Page Visibility Updates

1

User switches tab or minimizes

The browser marks the document as not visible to the user.

Trigger
2

document.hidden updates

Becomes true when hidden, false when visible again (MDN).

State
3

visibilitychange fires

Your listener reads document.hidden and adjusts behavior.

Event
4

Resume when visible

When the user returns, hidden is false — restart media, polling, or animations.

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • Part of the Page Visibility API with visibilityState and visibilitychange.
  • Read-only — you cannot assign to document.hidden.
  • Not identical to document.hasFocus() (window focus vs page visibility).
  • Related: head, body, ownerDocument, Document constructor.

Universal Browser Support

Document.hidden 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.hidden

Read-only boolean for Page Visibility — detect background tabs and hidden pages.

Universal 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 in legacy IE
Full support
Document.hidden Excellent

Bottom line: Listen for visibilitychange and read document.hidden to pause media, stop polling, and save resources when the user is not viewing the page.

Conclusion

Document.hidden is a simple, widely supported way to know whether your page is visible. Pair it with visibilitychange to pause media, stop timers, and build battery-friendly experiences.

Continue with images, head, body, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Listen for visibilitychange instead of polling
  • Pause media and heavy timers when document.hidden is true
  • Use visibilityState when you need prerender detail
  • Resume gracefully when the tab becomes visible again
  • Combine with user preferences (autoplay policies)

❌ Don’t

  • Assign to document.hidden — it is read-only
  • Assume focus and visibility are always the same
  • Keep aggressive polling running in hidden tabs
  • Forget mobile browsers also fire visibility events
  • Block critical saves solely on visibility without a fallback

Key Takeaways

Knowledge Unlocked

Five things to remember about document.hidden

Page Visibility in one boolean.

5
Core concepts
👁02

true

Tab hidden

Background
🔄03

Event

visibilitychange

MDN
🎬04

Pause

media / timers

UX
⚖️05

vs

visibilityState

Alternative

❓ Frequently Asked Questions

A boolean: true if the page is considered hidden (background tab, minimized window, or otherwise not visible to the user), false otherwise (MDN).
No. MDN marks Document.hidden as Baseline Widely available (since July 2015). It is part of the standard Page Visibility API.
document.hidden is a simple true/false. document.visibilityState returns a string such as visible, hidden, or prerender — MDN notes it as an alternative way to determine whether the page is hidden.
When the user switches tabs, minimizes the window, or the page is otherwise not visible — common cases for saving battery and pausing media.
The visibilitychange event on document. Read document.hidden inside the handler to react when visibility changes (MDN example).
No. It is a read-only property. You observe visibility; you cannot force the page hidden state from script.
Did you know?

Mobile browsers fire visibilitychange when users switch apps or lock the screen—not only when changing browser tabs. That makes document.hidden especially useful for saving battery on phones.

Next: images

Learn how to access every img element with document.images.

images →

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