Detect If Browser Tab Has Focus In Javascript

There are a few options we can use in order to Detect If Browser Tab Has Focus In Javascript.

Some of those options are:

1- Detect using visibilitychange event.

const handleVisibilityChange = function() {
    if (document.visibilityState === 'visible') {
        console.log('has focus');
    } else {
        console.log('lost focus');
    }
}
document.addEventListener("visibilitychange", handleVisibilityChange);

In the above code snippet, we’ve attached a listener to the event visibilitychange, and whenever the visibilitychange changed, the handleVisibilityChange function will be triggered and executed.

Inside the handleVisibilityChange function, we’ve a check on the value from document.visibilityState, if it’s equals to 'visible', means the tab is focused, otherwise it’s not.


2- Detect using document.hidden property.

const isFocused = () => typeof document.hidden !== undefined ? !document.hidden : null;

// Call/Use it
isFocused();

The above code snippet does the following:

  • Returns true if the current browser tab is focused.
  • Returns false if the current browser tab is not focused.
  • Returns null if document.hidden is not supported by the user’s browser.

3- Detect using blur/focus events.

function focused() {
    console.log('tab is focused');
}
function notFocused() {
    console.log('tab is not focused');
}

window.addEventListener('blur', notFocused);
window.addEventListener('focus', focused);

In the above code snippet, we’ve attached two listener functions to the events blur and focus.

When the tab is focused, the function focused will be executed in response to the focus event, and we will notice the log statement `console.log('tab is focused')` is executed and showing the message in the console.

Same happens, when the tab loses the focus, the function notFocused would be executed in response to the event blur , and the statement `console.log('tab is not focused')` would be executed.


That’s it for How To Detect If Browser Tab Has Focus In Javascript.

And as always, happy coding!!

Photo from Unsplash.

Related Posts

How to Capture Screenshots with Puppeteer In NodeJS

How to Capture Screenshots with Puppeteer In NodeJS

To Capture Screenshots with Puppeteer: Launch a Browser Instance Navigate to the Web Page Capture the Screenshot Introduction: Puppeteer is a powerful Node.js library that allows developers…

How to Minimize Puppeteer Browser Window To Tray

How to Minimize Puppeteer Browser Window To Tray

Puppeteer is a powerful tool for automating tasks in headless or non-headless web browsers using JavaScript. While Puppeteer is often used to perform actions within a browser,…

Intercepting Responses in Node.js with Puppeteer

Intercepting Responses in Node.js with Puppeteer

Introduction: Puppeteer is a powerful Node.js library that provides a high-level API for controlling headless Chrome or Chromium browsers. It’s widely used for web scraping, automated testing,…

Mastering React Component Re-rendering in Jest

Mastering React Component Re-rendering in Jest

In this hands-on guide, we’ll explore the art of optimizing React component re-rendering within Jest tests. By combining theory with practical coding examples, you’ll gain a deep…

Eliminating Nesting Loops in React Rendering

Eliminating Nesting Loops in React Rendering

React has ushered in a new era of web application development with its component-based structure, promoting code reusability and maintainability. But as projects evolve, achieving optimal performance…

Exploring Type and Interface Usage in TypeScript

Exploring Type and Interface Usage in TypeScript

TypeScript has gained immense popularity by bridging the gap between dynamic JavaScript and static typing. Two of its fundamental features, “Type” and “Interface,” play pivotal roles in…

Leave a Reply

%d bloggers like this: