How to Check if Browser Tab is Active in Javascript
In this tutorial, you will learn how to check if browser tab is active in javascript. There could be various scenarios where you would like to detect whether a user is active on your website or not.
A good example can be a downloading website that offers free and premium plans. If you are a premium user, then your download will start without any waiting period. But if you are a free or regular user, then you have to wait for a certain period of time before downloading can start.
For instance, the waiting period is 30 seconds. You might think why not just watch some Youtube video in another tab and when 30 seconds are over, the download will start automatically. But it would not happen since they will detect that you were not active on the website and the counter will freeze as soon as you switch tabs. It will resume automatically when you come back.
To accomplish our goal, we can make use of Page Visibility API. With the help of this, we can detect when a document becomes hidden or visible since the change in visibility triggers visibilitychange
event. I recommend you to have a look over MDN docs to learn more about it.
In the following example, we will pause or resume counter depending upon the visibility of the document. Please have a look over the code example and the steps given below.
HTML & CSS
- We have 2 elements in the HTML file (
div
andh1
). Thediv
element is just a wrapper for theh1
element. - The default
innerText
forh1
element is“0”
. - We have also included our javascript file
script.js
with ascript
tag at the bottom.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <div style="margin-top: 20px; text-align: center;"> <h1>0</h1> </div> <script src="script.js"></script> </body> </html>
Javascript
- We have selected the
h1
element usingdocument.querySelector()
method and stored it in thecounter
variable. - We have attached
DOMContentLoaded
event to thewindow
and in the event handler function, we are callingstartCounter()
method. - We have 2 global variables,
intervalId
andcounterValue
. The initial value forcounterValue
variable is 0. - In the
startCounter()
method, we are callingsetInterval()
method and passing it a delay of 1000 milliseconds. In the callback function, we are incrementing the value ofcounterValue
by 1 and setting it asinnerText
of theh1
element. We are storing the reference id of this interval in theintervalId
variable. - In the
stopCounter()
method, we are callingclearInterval()
method and passingintervalId
as a parameter. As a result, it will stop the currently running interval. - We have attached
visibilitychange
event to the document. In the event handler function, we are usingif
statement to check whether the document is hidden or not using thehidden
property. If true, we will executestopCounter()
method and return. If false, we will executestartCounter()
method.
let counter = document.querySelector('h1'); window.addEventListener('DOMContentLoaded', () => { startCounter(); }) let intervalId; let counterValue = 0; function startCounter(){ intervalId = setInterval(() => { counterValue++; counter.innerText = counterValue }, 1000); } function stopCounter(){ clearInterval(intervalId); } document.addEventListener('visibilitychange', () => { if(document.hidden){ stopCounter(); return; } startCounter(); })