Home Web Front-end H5 Tutorial Detailed introduction and usage examples of HTML5 visibilityState attribute_html5 tutorial skills

Detailed introduction and usage examples of HTML5 visibilityState attribute_html5 tutorial skills

May 16, 2016 pm 03:48 PM
html5

It must be explained here that this "activation" refers to whether this label is being browsed by the user, or whether it is the current label.

So, what exactly is this API used for? Usually, many traditional pages will continue to work when the user does not activate them. For example, when the user is browsing a news portal, the NBA game page he opened before will continue to refresh to get the latest results, and video websites will continue to occupy bandwidth. Loading resources, so if there is too much unnecessary work, it will cause a lot of waste of resources. Therefore, this product is quite useful:

1. The Web program will automatically update page information every once in a while to ensure that users get timely information. However, when users are browsing other pages, they can control it to pause the update.
2. Video websites will continue to load videos when playing online videos until the video is loaded. However, when users are browsing other pages, they can pause the loading of video resources to save bandwidth.
3. There is a large slideshow on the homepage of the website that automatically plays. When the user browses other pages, the playback can be paused.

So, through Page Visibility, we can achieve at least one or more of the following benefits:

1. Save server resources. Server resource usage such as Ajax polling is often ignored. Turning off this type of request can save resources.
2. Save memory consumption.
3. Save bandwidth consumption.

Therefore, using Page Visibility has benefits for both the user and the server.

The following is a formal introduction to this API. Page Visibility adds two properties hidden and visibilityState to the browser's document object. If the current tag is activated, the value of document.hidden is false , otherwise it is true . visibilityState has 4 possible values:

1.hidden: When the browser is minimized, tabs are switched, or the computer is locked, the visibilityState value is hidden
2.visible: When the document of the browser’s top-level context is displayed on at least one screen, visible is returned; when the browser window is not minimized but the browser is blocked by other applications, it is also visible
3.prerender: Returns prerender when the document is loaded outside the screen or invisible. This is a non-essential attribute and the browser can optionally support it.
4.unloaded: Returns unloaded when the document is about to be left (unload). The browser can also optionally support this attribute

In addition, a visibilitychange event will be added to the document, which is triggered when the visibility of the document changes.

Okay, after introducing the attributes, let’s put in a usage example (copy the code and save it to an HTML file, open it and switch tags to test the effect).


Copy code
The code is as follows:





Test HTML5 Page Visibility API


<script><br> function browerKernel(){<br> var result;<br> ['webkit', 'moz', 'o', 'ms'].forEach(function(prefix){</p> <p> if( typeof document[ prefix 'Hidden' ] != 'undefined' ){<br> result = prefix;<br> }<br> });<br> return result;<br> }<br> function init(){<br> prefix = browerKernel();<br> var showTip = document.getElementById('showTip');<br> document.addEventListener( prefix 'visibilitychange', function onVisibilityChange(e){<br> var tip = null; <br> if( document[ prefix 'VisibilityState' ] == 'hidden' ) tip = '<p>Leave page</p>';<br> else if( document[ prefix 'VisibilityState' ] == 'visible' ) tip = '<p>Enter page</p>';<br> showTip.innerHTML = showTip.innerHTML tip;<br> });<br> }<br> window.onload = init();<br> </script>

The purpose of this instance is to monitor whether the visibility of the label changes and generate a prompt when the visibility of the label changes.

It is worth noting that at present, browsers still support Page Visibility through private attributes, so when detecting or utilizing the attributes provided by Page Visibility, you need to add the browser private prefix, such as detecting the above in Chrome visibilityState property, you need to detect document.webkitVisibilityState instead of document.visibilityState. Therefore, the Demo will first detect the browser type and then use the Page Visibility API.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

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

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

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.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

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

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

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

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

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

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

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

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

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

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

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

See all articles