Use HTML5 to determine if a user is browsing page tips

Source: Internet
Author: User

Now, the HTML5 page visibility interface gives programmers a way to use Visibilitychange page events to determine the state of the current page visibility and to perform certain tasks in a targeted manner. There are also new Document.hidden properties that can be used.

Document.hidden

This new Document.hidden property, which shows whether the page is the page that the user is currently viewing, with a value of ture or false.

Document.visibilitystate

The value of visibilitystate is either visible (indicates that the page is currently active tab for the browser, and the window is not minimized), or hidden (the page is not the active tab page, or the window is minimized.) ), or PreRender (the page is regenerated, not visible to the user.) ).

VIsibilitychange Event

Monitoring page Visibility changes is easy:

The code is as follows:

  1. Various browser compatible
  2. var hidden, state, visibilitychange;
  3. if (typeof Document.hidden!== "undefined") {
  4. Hidden = "hidden";
  5. Visibilitychange = "Visibilitychange";
  6. State = "Visibilitystate";
  7. } Else if (typeof Document.mozhidden!== "undefined") {
  8. Hidden = "Mozhidden";
  9. Visibilitychange = "Mozvisibilitychange";
  10. State = "Mozvisibilitystate";
  11. } Else if (typeof Document.mshidden!== "undefined") {
  12. Hidden = "Mshidden";
  13. Visibilitychange = "Msvisibilitychange";
  14. State = "Msvisibilitystate";
  15. } Else if (typeof Document.webkithidden!== "undefined") {
  16. Hidden = "Webkithidden";
  17. Visibilitychange = "Webkitvisibilitychange";
  18. State = "Webkitvisibilitystate";
  19. }//Add listener to display status changes in title
  20. Document.addeventlistener (Visibilitychange, function () {
  21. Document.title = Document[state];
  22. }, false); //Initialize
  23. Document.title = Document[state];


The above code modifies the value of document.title when the visibility of the page changes!

So, when do you need to use the Visibilitychange event? For example, if you have an embedded video on your page that is playing, when the user switches to another tab, the video on your tab should automatically pause and continue playing when the user switches back. For example, if your page has an automatic refresh action, when the user switches to another tab, you should stop the refresh and continue the previous action when the user switches back.

Reprint Please specify: Code Academy >Web Front end > HTML tutorial > How to use HTML5 to determine whether a user is viewing a page

Use HTML5 to determine if a user is browsing page tips

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.