Page Visibility API in JavaScript
Report tab visibility in JavaScript using document.hidden and visibilitychange. The page and console show state; a visible tab does not prove attention.
Save this as index.html and open it in a browser.
1<!doctype html>2<html lang="en">3<meta charset="utf-8">4<title>JavaScript practice</title>5<body>6<p id="status"></p>7<script>8const status = document.querySelector('#status');9function reportVisibility() {10 status.textContent = document.hidden ? 'Revision paused: tab hidden' : 'Revision active: tab visible';11 console.log(status.textContent);12}13document.addEventListener('visibilitychange', reportVisibility);14reportVisibility();15</script>16</body>17</html>
Sample Output
Revision active: tab visible
The document is not hidden. This says nothing about a person paying attention.
Revision paused: tab hidden
The tab is hidden. This lesson reports the state; it does not pause a real timer.
In this one
- Read the initial visibility state
- Listen for visibilitychange
- Avoid guessing tab state
- Separate a status message from pause logic
How it works
- 1
Find the paragraph used for status.
- 2
Make one function read document.hidden.
- 3
Register that function for visibilitychange.
- 4
Call it once immediately for the initial state.
- 5
Switch tabs and return to see visible, hidden, then visible messages.
Edge cases
No initial event
The page can load without a visibilitychange event.
Call the reporting function during setup so the paragraph is not empty until the first tab switch.
Timer throttling
Background timers can run less often.
Do not count timer callbacks to calculate elapsed time. Visibility helps decide when to pause, but it does not guarantee timer timing.
What's going on
The Page Visibility API tells a page whether the browser considers it visible or hidden. An API is a browser feature that code can use. A revision app can use this signal to pause animations when its tab is in the background.
This browser-only page prints the initial state and listens for visibilitychange. The handler reads document.hidden each time rather than guessing by flipping a variable. The same text goes to the page and console, so you can compare them when you return to the tab.
The message says paused, but this small example only reports state; it does not run or stop a timer. In a real app you would put pause logic in the hidden branch. Visibility is not proof that someone is paying attention. A visible tab may be covered partly or left unattended, so do not use it to grade attendance.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Why call reportVisibility at the end?
Pick an answer to check it.
Question 2
Which value is true for a hidden tab?
Pick an answer to check it.
Questions people ask
Is hidden the same as losing keyboard focus?
No. A visible page can lose focus. Page visibility describes whether the document is considered visible.
Will this pause my video automatically?
No. You must call the video pause method in the handler if that is the behavior you want.
Can this prove that a student watched a lesson?
No. Visibility is a page state, not a measure of attention or learning.