How to Use PageVisibilityAPI for page switch activation in HTML5-

Source: Internet
Author: User
This article mainly introduces the use of PageVisibilityAPI for page switch activation in HTML5. PageVisibility allows us to automatically stop and play videos when switching the browser tab page, for more information, see the HTMl5 API Page Visibility, this API focuses on a feature that is rarely noticed-whether the browser tab is activated. It must be explained that the "activation" refers to whether the tag is being viewed by the user or whether it is the current tag.

So what is the purpose of this API? In general, many traditional pages continue to work when the user does not activate them. For example, when the user is browsing the news portal, the previously opened NBA game page will continue to refresh the latest results, and video websites will continue to use bandwidth to load resources. Therefore, if this type of unnecessary work is too much, this will cause a lot of waste of resources. Therefore, this product is quite useful:

The Web application automatically updates the page information at intervals to ensure that the user obtains the timely information. However, when the user is browsing other pages, it can be controlled to pause updates.
When you play an online video, the video is continuously loaded until the video is loaded. However, when you are browsing other pages, you can pause loading video resources to save bandwidth.
The homepage of the Website Supports automatic playback of large slides. When you browse other pages, you can pause playback.
Therefore, with Page Visibility, we can achieve at least one or more of the following benefits:

1. Saving server resources. Ajax round-robin is often ignored for this type of server resources. Disabling such requests can save resources.
2. Saves memory consumption.
3. save bandwidth consumption.

Therefore, using Page Visibility is good for both users and servers.

Next we will officially introduce this API. Page Visibility adds two properties, hidden and visibilityState, to the document Object of the browser. If the current tag is activated, the value of document. hidden is false; otherwise, the value is true. VisibilityState has four possible values:

Hidden: the value of visibilityState is hidden when the browser is minimized, the tag is switched, and the screen is locked by the computer.
Visible: if the document of the context in the browser is displayed on at least one screen, visible is returned. If the browser window is not minimized, but the browser is blocked by other applications, this is also visible.
Prerender: The prerender is returned when the document is loaded outside or invisible to the screen. This is an optional attribute and is supported by the browser.
Unloaded: unloaded is returned when the document is about to be removed (unload). The browser can also selectively support this attribute.
In addition, a visibilitychange event is added to the document, which is triggered when the visibility of the document changes.

Okay. After introducing the attributes, put a Demo (switch the tag to test ).

This Demo monitors whether the visibility of tags changes and generates a prompt when the visibility of tags changes.

It is worth noting that at present, browsers still support Page Visibility through private attributes. Therefore, when detecting or using the properties provided by Page Visibility, you must add a private browser prefix, for example, when detecting the visibilityState attribute in Chrome, you need to check the document. webkitVisibilityState instead of document. visibilityState. Therefore, the Demo checks the browser type before using the Page Visibility API. The main code is as follows:

Function browerKernel () {var result; ['webkit', 'moz', 'O', 'ms']. forEach (function (prefix) {if (typeof document [prefix + 'den den ']! = 'Undefined') {result = prefix ;}}); return result ;}function init () {prefix = browerKernel (); var showTip = document. getElementById ('showtip '); document. addEventListener (prefix + 'visibilitychang', function onVisibilityChange (e) {var tip = null; if (document [prefix + 'visibilitystate'] = 'den den ') tip ='

Exit page

'; Else if (document [prefix + 'visibilitystate'] = 'visible') tip ='

Go to the page

'; ShowTip. innerHTML = showTip. innerHTML + tip;}) ;}window. onload = init ();

Let's look at a specific example. The Code is as follows:

                          

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.