Visibility.js is a wrapper for the Page Visibility API. It hides vendor prefixes and adds high level functions.
Moreover, you can detect if the browser is just prerendering the page while the user has still not opened the link, and don’t count this as a visit in your analytics module, or do not run heavy calculations or other actions which will disable the prerendering.
Page Visibility API is natively supported by all browsers. For old browsers
you can use
lib/visibility.fallback.js with focus/blur hack (note that this
hack has an issue: when browser just lose focus but still visible for user, its state will change to [hidden]).
Currently the Page Visibility API supports three visibility states:
visible: user has opened the page and works within it.
hidden: user has switched to another tab or minimized browser window.
prerender: browser is just prerendering a page which may possibly be opened by the user to make the apparent loading time smaller.
The main use case for this library is to enable some of the times only when content is visible to the user, i.e. the ones animating a countdown animation.
Visibility.every(interval, callback) is similar to
setInterval(callback, interval), but calls
interval ms only
if the page is visible. For example, let’s create a countdown timer:
You can provide an additional interval which will be used when the page is hidden. In next example, a check for inbox updates will be run every 1 minute for a visible page and every 5 minutes for a hidden one:
var minute = 60 * 1000;Visibility;
When the page becomes visible, if the callback has not been called in longer than
the visible interval, it will be called immediately. In the example above, if you
hid the page for 9 minutes,
checkForEmail will get called once while the page is hidden,
and immediately when it is made visible.
Visibility.every returns a timer identifier, much like the
function. However, it cannot be passed to
clearInterval, and you should use
Visibility.stop(id) to stop the timer.
var slideshow = Visibility;;
If the browser does not support the Page Visibility API,
fall back to
callback will be run every
interval ms for
both the hidden and visible pages.
Another common use case is when you need to execute some actions upon a switch to particular visibility state.
Waiting until the page becomes visible
Visibility.onVisible(callback) checks current state of the page. If it is
visible now, it will run
callback, otherwise it will wait until state changes
visible, and then run
For example, let’s show an animated notification only when the page is visible, so if some user opens a page in the background, the animation will delay until the page becomes visible, i.e. until the user has switched to a tab with the page:
If a browser doesn’t support Page Visibility API,
will run the
Wait until the page is opened after prerendering
A web developer can hint a browser (using Prerendering API) that an user is likely to click on some link (i.e. on a “Next” link in a multi-page article), and the browser then may prefetch and prerender the page, so that the user will not wait after actually going via the link.
But you may not want to count the browser prerendering a page as a visitor in your analytics system. Moreover, the browser will disable prerendering if you will try to do heavy computations or use audio/video tags on the page. So, you may decide to not run parts of the code while prerendering and wait until the user actually opens the link.
You can use
Visibility.afterPrerendering(callback) in this cases. For example,
this code will only take real visitors (and not page prerenderings) into
If the browser doesn’t support Page Visibility API,
Visibility.afterPrerendering will run
In some cases you may need more low-level methods. For example, you may want to count the time user has viewed the page in foreground and time it has stayed in background.
Visibility.isSupported() will return
true if browser supports the
Page Visibility API:
Visibility.state() will return a string with visibility state. More states
can be added in the future, so for most cases a simpler
method can be used. It will return
true if the page is hidden by any reason.
For example, while prerendering,
Visibility.state() will return
Visibility.hidden() will return
This code will aid in collecting page visibility statistics:
And this example will only enable auto-playing when the page is opening as a visible tab (not a background one):
Visibility.change(callback) you can listen to visibility state changing
callback takes 2 arguments: an event object and a state name.
Let’s collect some statistics with this events approach:
change returns listener ID. You can use it to unbind listener by
var listener = Visibility;VideoPlayer;
afterPrerendering will also return listener ID,
if they wait visibility state changes. If they execute callback immediately,
true if Page Visibility API is supported and
if they can’t detect visibility state.
var listener = Visibility;notification;
Visibility.js is shipped with 4 files:
visibility.core– core module.
stopmethods to set
setIntervaldepend on visibility state.
visibility.fallback– fallback for browser without Page Visibility API. It use document
blurevents, so document become to be hidden, when browser just lose focus, but still visible for user.
Available by NPM:
npm install --save visibilityjs
To run tests you need node.js and npm. For example, in Ubuntu run:sudo apt-get install nodejs npm
Next install npm dependencies:npm install
Run all tests:npm test
Also you can see real usage example in integration test