1var hidden, visibilityChange;
2if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support
3 hidden = "hidden";
4 visibilityChange = "visibilitychange";
5} else if (typeof document.msHidden !== "undefined") {
6 hidden = "msHidden";
7 visibilityChange = "msvisibilitychange";
8} else if (typeof document.webkitHidden !== "undefined") {
9 hidden = "webkitHidden";
10 visibilityChange = "webkitvisibilitychange";
11}
12function handleVisibilityChange() {
13 if (document[hidden]) {
14 // Do here something if page is hidden (not in focus)
15 } else {
16 // Do here something if page is in focus
17 }
18}
19if (typeof document.addEventListener === "undefined" || hidden === undefined) { // Skip if the browser doesn't support addEventListener or the Page Visibility API
20} else {
21 document.addEventListener(visibilityChange, handleVisibilityChange, false); // Handle page visibility change
22}