Skip to main content

Checks if the browser tab of the page is focused.

/**
 * Checks if the browser tab of the page is focused.
 *
 * Use the Document.hidden property, introduced by the Page Visibility API to
 * check if the browser tab of the page is visible or hidden.
 *
 * https://www.30secondsofcode.org/js/s/is-browser-tab-focused
 *
 * @return {Boolean}
 */
const isBrowserTabFocused = () => !document.hidden;

// Examples

isBrowserTabFocused(); // true

// -------------------------------------------------------------
// ES5 version
// -------------------------------------------------------------

var isBrowserTabFocused = function () {
  return !document.hidden;
};