If you are browsing website which is displaying some kind of real time data like football score or cricket score or something like that, and also want to check your facebook wall. As soon as you switch the browser tab you miss the moment. What if you can check live score while browsing other websites ? Yes thats possible now with page visibility api. You can now find if browser tab has focus or not.
I have listed both the versions for checking browser tab visibility using plain javascript and jquery. Jquery version has many advantages over plain javascript version its small, simple, light weight and you dont have to worry about the compatibility, jquery team takes care of that, Isnt it cool ?
I have listed both the versions for checking browser tab visibility using plain javascript and jquery. Jquery version has many advantages over plain javascript version its small, simple, light weight and you dont have to worry about the compatibility, jquery team takes care of that, Isnt it cool ?
Checking browser tab visibility using plain javascript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
tab_visibility = function(){ var hidden, state, visibilityChange; if (typeof document.hidden !== "undefined") { state = "visibilityState"; } else if (typeof document.mozHidden !== "undefined") { state = "mozVisibilityState"; } else if (typeof document.msHidden !== "undefined") { state = "msVisibilityState"; } else if (typeof document.webkitHidden !== "undefined") { state = "webkitVisibilityState"; } return document[state]; } |
Checking browser tab visibility using jquery
1 2 3 4 5 6 |
$(window).blur(function(){ document.title = 'Hidden'; }); $(window).focus(function(){ document.title = 'Visible'; }); |
A demo would be great start