Page Visibility shim for jQuery
This plugin gives you a very simple API that allows you to execute callbacks when the page’s visibility state changes.
It does so by using the Page Visibility API where it’s supported, and falling back to good old
blur in older browsers.
When to use?
Typical use cases include but are not limited to pausing/resuming slideshows, video, and/or embedded audio clips.
This plugin simply provides two custom document events for you to use:
hide. When the page visibility state changes, the appropriate event will be triggered.
You can use them separately:
For most applications you'll need both events, so the most convenient option is to use an events map. This way, you can bind both event handlers in one go:
Or bind both to the same callback and distinguish using the event variable.
The plugin will detect if the Page Visibility API is natively supported in the browser or not, and expose this information as a boolean (
$.support was marked deprecated in jQuery version 1.9, so it is likely to be removed in the future.
if $supportpageVisibility// Page Visibility is natively supported in this browser
If the Page Visibility API is supported the plugin will also store the current visibility state in
if !documenthidden// Page is currently visible
This plugin is not a Page Visibility polyfill, as it doesn’t aim to mimic the standard API. It merely provides a simple way to use this functionality (or a fallback) in your jQuery code.
This plugin is available under the MIT license.