Git Product home page Git Product logo

visibly.js's Introduction

visibly.js

A shim for the Page Visibility API

visibly lets you easily establish whether a page currently being viewed in the browser is visible to the user or has been hidden by them switching to another tab.

The visibly polyfill wraps around the Page Visibility API defined by the W3C. Native support for the API can be found in all modern browsers based on data from caniuse.com.

Demo

See the demo for a quick preview of the shim in action.

Why care about page visibility?

Creating user experiences around page visibility detection allows one to improve performance by opting to do things such as stopping data-streams/AJAX requests from being pulled should they decide to switch tabs. Data-flow can then be resumed when a user tabs back with very little visible cost to the experience.

Creative uses such as pausing video streams until a user returns, changing content depending on their tab behaviour or even saving interim versions of content being written in the browser (such as with GMail) are also possibly use-cases.

##Usage examples

###Polyfilled methods based on the official specifications

// visibilityChange()
visibly.visibilitychange(function(state){
	console.log('The current visibility state is:' + state);
});

// hidden()
if(visibly.hidden()){
	console.log('the current page is hidden')
}

// visibilityState()
console.log('The current visibility state is:' + visibly.visibilityState());

Performing an action when the page is visible or hidden

visibly.onVisible(callback) allows you to trigger a callback when a page switches from a hidden state to a visible state. Similarly visibly.onHidden(callback) performs the converse.

The following is an example where both the document title will be altered based on the page state and a message will be logged to the browser's console based on the same.

visibly.onVisible(function () {
    console.log('page is visible');
});

visibly.onHidden(function () {
    console.log('page is hidden');
});

visibly.onVisible(function (){
	document.title = 'visible';
});

visibly.onHidden(function (){
	document.title = 'hidden';
});

Other features

visibly.isSupported() will return true if browser supports the Page Visibility API natively.

if(visibly.isSupported()) {
    console.log('page visibly natively supported');
}

Compatibility & Size

visibly is approx. 2K (minified) and has been tested with all Chrome release channels (36+), Opera stable, Safari 7.1, Firefox stable & nightlies, Internet Explorer 8.0, 9.0 and 10PP2.

Alternatives

An alternative to the visibly polyfill is visibility.js. This solution offers a similar set of features but for an additional 1.5KB you are also able to get time-related features should you need them. For more on this solution see: https://github.com/ai/visibilityjs

visibly.js's People

Contributors

addyosmani avatar andyperlitch avatar mgechev avatar desau avatar

Stargazers

Andy Lu avatar lyqline avatar S avatar __qms avatar Chris avatar KDTS avatar  avatar  avatar Zachariah Moreno avatar Opeyemi Obembe avatar Yasin ATEŞ avatar Mustafa Demirkent avatar Michał Kędrzyński avatar  avatar vlad avatar abhishek.saware avatar Souhaib avatar HAO GONG avatar Andyjin avatar Jeff Matthews avatar Greg ORIOL avatar Will Bjorn avatar Alejandro Esquivel avatar Ivan avatar Blaž Peruš avatar Momo avatar ilhan avatar luckyhu avatar Paul Anthony avatar caixin avatar orkhan muradov avatar Bruce Sundberg avatar  avatar Nauman Tariq avatar Kyungwook, Park avatar Yingjun Xu avatar Jeong Seong Dae avatar Monyer avatar Cuity avatar  avatar Mojtaba Espari Pour avatar Simon Lin avatar  avatar 流螢 avatar Hsiang avatar Dave avatar Mohamed El Bahja avatar Hosein avatar Ian Hung avatar  avatar Fabio Dias Rollo avatar Gopal Sharma avatar Jay Fraser avatar Richard Gaunt avatar Jonathan H avatar Kartashov Alexey avatar 胡继伟 avatar  avatar Stas Beloshkurov avatar laoyao avatar  avatar Jeremy Sarda avatar Aleks avatar  avatar 巫书轶 avatar Jkin8010 avatar Drew avatar Mateus Henrique Calza avatar Samar Panda avatar Booker Zhao avatar Mahmoud Sami avatar Jihan Kim avatar Volkan Gemicioğlu avatar Wladimir Coka avatar Jessica Claire Edwards avatar Devesh Kumar avatar Terkel avatar Howard Zheng avatar   ヨシュア avatar  avatar Steffen Meyer avatar Humanismusic avatar Celso avatar Exequiel Ceasar Navarrete avatar Didik Wicaksono avatar Jassi avatar Prayag Verma  avatar t1st3 avatar Eren Hatırnaz avatar  avatar Igor Ovsiannikov avatar Susan Mosby avatar Matthias Treitler avatar  avatar Sambath Kumar Logakrishnan avatar Whoami avatar Angus H. avatar Mark Scripter avatar Fazlur Rahman avatar Ivan Kurcubic avatar

Watchers

 avatar LC avatar James Cloos avatar t1st3 avatar Michael Anthony avatar Amit Kanfer avatar Whoami avatar  avatar

visibly.js's Issues

Title not changing

Curious... when I cloned and ran visibly.html, the console.log() calls work but the title doesn't change.

I am on Chrome 32.0.1700.102.

same Event Firing multiple time.

visibly.onVisible and visibly.onHidden both are firing multiple time. I am working with socket, I have to disconnect socket on hidden and connect socket again on visible. But, due to multiple occurrence I am getting issue of multiple socket ids. I can not change Socket Implementation as of now.

But, what I want is to limit multiple execution to single time only.

Is there any idea, why this is happening, I have only single callback set up for both events.

Any type is really appreciable. Thanks

Licence

@addyosmani Could you please add a licence type for this shim. Really appreciate it.

firefox warning

Firefox version 49 yells about this:
'mozHidden' and 'mozVisibilityState' are deprecated. Please use the unprefixed 'hidden' and 'visibilityState' instead.

can you please fix this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.