


How to determine whether the user is browsing the page in HTML5_html5 tutorial skills
Now, the page visibility interface in HTML5 provides programmers with a method that allows them to use the visibilitychange page event to determine the visibility status of the current page and perform certain tasks in a targeted manner. There is also a new document.hidden property available.
document.hidden
This new document.hidden attribute shows whether the page is the page currently viewed by the user. The value is true or false.
document.visibilityState
The value of visibilityState is either visible (indicating that the page is the currently activated tab of the browser, and the window is not minimized), or hidden (the page is not the currently activated tab page, or the window is minimized.), or prerender (the page Regenerating, not visible to the user ).
visibilitychange event
Listening to page visibility changes is very easy:
// Compatible with various browsers
var hidden, state, visibilityChange;
if (typeof document.hidden !== "undefined") {
hidden = "hidden";
visibilityChange = "visibilitychange";
state = "visibilityState";
} else if (typeof document.mozHidden !== "undefined") {
hidden = "mozHidden";
visibilityChange = "mozvisibilitychange";
state = "mozVisibilityState";
} else if (typeof document.msHidden !== "undefined") {
hidden = "msHidden";
visibilityChange = "msvisibilitychange";
state = "msVisibilityState";
} else if (typeof document.webkitHidden !== "undefined") {
hidden = "webkitHidden";
visibilityChange = "webkitvisibilitychange";
state = "webkitVisibilityState";
}
//Add a listener to display status changes in the title
document.addEventListener(visibilityChange, function() {
document.title = document[state];
}, false);
//Initialization
document.title = document[state];
The above code will modify the value of document.title when the page visibility changes!
So, when do you need to use the visibilitychange event? For example, if you have an embedded video playing on your page, when the user switches to another tab, the video on your tab should automatically pause and resume playing when the user switches back. For another example, if your page has an automatic refresh action, when the user switches to another tab, you should stop refreshing, and continue the previous action when the user switches back.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.
