DEV Community

Cover image for Detect When Users Switch Tabs using JavaScript

Detect When Users Switch Tabs using JavaScript

Jatin Sharma on September 07, 2022

In this article, I'll show you how to find out when users change tabs in browsers using JavaScript. It's going to be fun, it's helpful to see how o...
Collapse
 
edgaralves profile image
Edgar Alves

Just a side note, the "blur" event will trigger if you leave the tab opened but just focus another app on your machine... while the visibilitychange will only trigger if you switch to another tab.

Collapse
 
j471n profile image
Jatin Sharma

I didn't know that, thanks for the note I'll look into it ✨

Collapse
 
mrdulin profile image
official_dulin

Real world usage: react-query Window Focus Refetching

Collapse
 
j471n profile image
Jatin Sharma

Thanks for this mate :)

Collapse
 
wisdomsamuel7 profile image
Wisdom-Samuel7

Simple and a Great one

Collapse
 
j471n profile image
Jatin Sharma

Thanks.

Collapse
 
lenardo profile image
Lenardo de Bruine

Thanks! That's useful for example to trigger people to come back :).
You can save the meta document title so when a user come's back the normall meta title is displayed.

        var documentTitle = document.title;

        window.addEventListener("blur", function() {
            document.title = "👋 Hi, come back!";
        });

        window.addEventListener("focus", function() {
            document.title = documentTitle;
        });
Enter fullscreen mode Exit fullscreen mode
Collapse
 
j471n profile image
Jatin Sharma

Yes, you can do that. You can also send the notifications to the user to come back using Notifications API

Collapse
 
sonia58073354 profile image
Sonia

We can track the tab switch but there will be an issue when the user presses the back button.

Collapse
 
j471n profile image
Jatin Sharma

Well, it depends if you have implemented this functionality on the whole website or a single page. There could be two cases-

  • When the user presses the back button and leaves the website. In this case, we can't do anything.
  • When the user presses the back button but stays on the same website then it will load the initial status of the page (visible) as the user hasn't left the tab.
Collapse
 
naucode profile image
Al - Naucode

Thank you for the post, bookmarked!

Collapse
 
j471n profile image
Jatin Sharma

That's great. It's my pleasure ☺️

Collapse
 
iarchitsharma profile image
Archit Sharma

Thank you so much for this awesome article

Collapse
 
j471n profile image
Jatin Sharma

This is a pleasure, the positive feedbacks are always inspiring.

Collapse
 
tapive profile image
Gary • Edited

You can use this to evaluate the quality of your articles. If the user switches between tabs, they may not like your article. But I wouldn't change the tab title - it's annoying.

Collapse
 
j471n profile image
Jatin Sharma

Changing the title is all about showing that it works.I won't change title either.

Collapse
 
faizal97 profile image
Faizal Ardian Putra

Cool! Great!

Collapse
 
j471n profile image
Jatin Sharma

Thanks mate 😊

Collapse
 
faizal97 profile image
Faizal Ardian Putra

Cool!

Collapse
 
andrewbaisden profile image
Andrew Baisden

Thanks for sharing this.

Collapse
 
j471n profile image
Jatin Sharma

It's always a pleasure :)

Collapse
 
umamahesh_16 profile image
Umamaheswararao Meka

Very Good, I never realized it can be this simple, Thanks for this !!!

Collapse
 
jenniesworth12 profile image
Jennie Sworth

That's great.

Collapse
 
sohamgupta profile image
Soham Gupta

Omg I didn't know this was even possible, thanks for sharing it.

Collapse
 
j471n profile image
Jatin Sharma

Yes it is possible, And now you know too. :)

Collapse
 
vijay_dev profile image
vijay banjara

😮

Collapse
 
j471n profile image
Jatin Sharma

Why are you in shock 🤔

Collapse
 
lunaisakh profile image
Luna Isakh

Thank you for sharing the post. Let me bookmarked it

Collapse
 
nepster23 profile image
Rojil Shrestha

Really appreciate..

Collapse
 
pranav2580 profile image
Pranav Kumar

Amazing 😅 dude

Collapse
 
j471n profile image
Jatin Sharma

Thanks mate :)