Book Image

Mastering jQuery

By : Alex Libby
Book Image

Mastering jQuery

By: Alex Libby

Overview of this book

<p>Mastering jQuery has been written not only to help maximize your skills with core functionality in the library, but also to explore some of the more intriguing ways of using the library to achieve real-world solutions that could feature on any website or online environment.</p> <p>You'll start with a look at some of the more advanced ways to incorporate the library into your pages, followed by working with forms and advanced form validation using regular expressions. Next you'll move on to animating in jQuery, advanced event handling, and using jQuery effects.</p> <p>Finally, you will develop practical examples of using jQuery with external functionality such as node-webkit, before finishing with a session on optimizing your version of the library for maximum efficiency and exploring best practices for using QUnit.</p>
Table of Contents (21 chapters)
Mastering jQuery
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Implementing the Page Visibility API


So far, we've been introduced to the Page Visibility API, and have covered the benefits of using it to pause content when it is not visible. It's worth spending a moment to see how we can implement it in our code, and how such a simple change can reap massive benefits.

We'll begin with covering the plain JavaScript first, before looking at using jQuery later in the chapter:

  1. Let's start by extracting the markup files we need from the code download that accompanies this book. For this demo, we'll need basicuse.html and basicuse.css. Save the files into the root and css subfolders of our project area respectively.

  2. Next, in a new file add the following code:

    function log(msg){
      var output = document.getElementById("output");
      output.innerHTML += "<li>" + msg + "</li>";
    }
    
    window.onload = function() {
      var hidden, visibilityState, visibilityChange;
      if (typeof document.hidden !== "undefined") {
        visibilityChange = "visibilitychange";
      }
      document...