| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ru/docs/Web/API/Page_Visibility_API | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..
, web . Page Visibility API , , , , .
, API visibilitychange . - . , , , ( ), , . , , .
<iframe> . <iframe> CSS ( display: none;) , .
Page Visibility API.
. , blur focus window , . Page Visibility API .
Separately from the Page Visibility API, user agents typically have a number of policies in place to mitigate the performance impact of background or hidden tabs. These may include:
requestAnimationFrame() callbacks to background tabs or hidden <iframe>s in order to improve performance and battery life.setTimeout() are throttled in background/inactive tabs to help improve performance. See Reasons for delays longer than specified for more details.Some processes are exempt from this throttling behavior. In these cases, you can use the Page Visibility API to reduce the tabs' performance impact while they're hidden.
View live example (video with sound).
The example, which pauses the video when you switch to another tab and plays again when you return to its tab, was created with the following code:
// Set the name of the hidden property and the change event for visibility
var hidden, visibilityChange;
if (typeof document.hidden !== "undefined") {
// Opera 12.10 and Firefox 18 and later support
hidden = "hidden";
visibilityChange = "visibilitychange";
} else if (typeof document.msHidden !== "undefined") {
hidden = "msHidden";
visibilityChange = "msvisibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
hidden = "webkitHidden";
visibilityChange = "webkitvisibilitychange";
}
var videoElement = document.getElementById("videoElement");
// If the page is hidden, pause the video;
// if the page is shown, play the video
function handleVisibilityChange() {
if (document[hidden]) {
videoElement.pause();
} else {
videoElement.play();
}
}
// Warn if the browser doesn't support addEventListener or the Page Visibility API
if (typeof document.addEventListener === "undefined" || hidden === undefined) {
console.log(
"This demo requires a browser, such as Google Chrome or Firefox, that supports the Page Visibility API.",
);
} else {
// Handle page visibility change
document.addEventListener(visibilityChange, handleVisibilityChange, false);
// When the video pauses, set the title.
// This shows the paused
videoElement.addEventListener(
"pause",
function () {
document.title = "Paused";
},
false,
);
// When the video plays, set the title.
videoElement.addEventListener(
"play",
function () {
document.title = "Playing";
},
false,
);
}
The Page Visibility API adds the following properties to the Document interface:
Document.hidden Returns true if the page is in a state considered to be hidden to the user, and false otherwise.
Document.visibilityState A String indicating the document's current visibility state. Possible values are:
visibleThe page content may be at least partially visible. In practice this means that the page is the foreground tab of a non-minimized window.
hiddenThe page's content is not visible to the user, either due to the document's tab being in the background or part of a window that is minimized, or because the device's screen is off.
prerenderThe page's content is being prerendered and is not visible to the user. A document may start in the prerender state, but will never switch to this state from any other state, since a document can only prerender once.
unloadedThe page is in the process of being unloaded from memory.
Document.onvisibilitychangeAn EventListener providing the code to be called when the visibilitychange event is fired.
//startSimulation and pauseSimulation defined elsewhere
function handleVisibilityChange() {
if (document.hidden) {
pauseSimulation();
} else {
startSimulation();
}
}
document.addEventListener("visibilitychange", handleVisibilityChange, false);
| Specification |
|---|
| HTML # dom-document-visibilitystate |
This page was last modified on 27 . 2026 . by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |