javascript if browser out of focus code example

Example: javascript if browser out of focus

var hidden, visibilityChange;
if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support
  hidden = "hidden";
  visibilityChange = "visibilitychange";
} else if (typeof document.msHidden !== "undefined") {
  hidden = "msHidden";
  visibilityChange = "msvisibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
  hidden = "webkitHidden";
  visibilityChange = "webkitvisibilitychange";
}
function handleVisibilityChange() {
  if (document[hidden]) {
  	// Do here something if page is hidden (not in focus)
  } else {
    // Do here something if page is in focus
  }
}
if (typeof document.addEventListener === "undefined" || hidden === undefined) { // Skip if the browser doesn't support addEventListener or the Page Visibility API
} else {
  document.addEventListener(visibilityChange, handleVisibilityChange, false); // Handle page visibility change
}

Tags:

Misc Example