Determine if a Tab has Focus in JavaScript

Tyler Tyler (285)
Total time: 2 minutes 
Updated: July 22nd, 2020

Tabbed browsing has become the standard so modern browsers have provided a way for developers to determine if a tab has focus. This uses the HTML 5 Page Visibility API. Different browsers currently use different implementations, but this guide should help simplify things for you.

Posted in these interests:

javascript
PRIMARY
27 guides
document.hiddendocument.hidden

The first and most simple thing this API provides is a property on the document that lets you know if the page is currently hidden. To run a quick test, open the console on this very page (cmd + option + j on Mac) and type:

document.hidden

As expected this should print:

false

Obviously, the page you are currently viewing is not hidden. But now type this into your console:

setTimeout(function() { console.log(document.hidden); }, 4000);

and immediately switch to another tab. Wait at least four seconds and switch back to this tab. You will now see:

true

This is because the page was hidden when the timeout callback fired.

There are three possible visibility states: hidden, visible, and prerender. hidden and visible are self-explanatory, and prerender is simply the state before the page is fully rendered while the tab is in focus.

This is the event that is fired whenever the browser comes into or goes out of focus.

document.addEventListener('visibilitychange', function(e) { console.log(document.hidden); });

The following code is a modification of what you’ll find at the Mozilla Developer Network documentation for the Page Visibility API.

// Set the name of the hidden property and the change event for visibility var hidden, visibilityChange; if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support hidden = "hidden"; visibilityChange = "visibilitychange"; state = "visibilityState"; } else if (typeof document.mozHidden !== "undefined") { hidden = "mozHidden"; visibilityChange = "mozvisibilitychange"; state = "mozVisibilityState"; } else if (typeof document.msHidden !== "undefined") { hidden = "msHidden"; visibilityChange = "msvisibilitychange"; state = "msVisibilityState"; } else if (typeof document.webkitHidden !== "undefined") { hidden = "webkitHidden"; visibilityChange = "webkitvisibilitychange"; state = "webkitVisibilityState"; }

We need to determine the correct attribute names based on the browser. So utilizing the code above we will access our hidden property like this:

document[hidden]
as opposed to:
document.hidden
This will help keep our code compatible across browsers. Whenever a user changes tabs and the visibility state changes, an event is fired. To listen to the event you can do the following:
document.addEventListener(visibilityChange, function() { console.log(document[hidden]); });

With the code above, you can switch tabs and it will log the state to the console.

Check out our page-visibility module on Github. We will probably expand this over time, but the main purpose is to abstract the code that makes this thing cross browser compatible. So with our module, instead of doing this:

document[hidden]

or

document.hidden

you can do:

Visibility.isHidden()

And you can also get the event name easily:

document.addEventListener(Visibility.visibilitychange, function() { console.log(Visibility.isHidden()); });

And if you want your callback to run only the first time the page becomes visible you can do this:

document.addEventListener(Visibility.visibilitychange, function(e) { var hidden = Visibility.isHidden(); if (!hidden) { console.log('This first only the first time the page is visible!!'); e.target.removeEventListener(e.type, arguments.callee); } });

Determine if a Tab has Focus in JavaScript

Tyler Tyler (285)
Total time: 2 minutes 
Updated: July 22nd, 2020

Tabbed browsing has become the standard so modern browsers have provided a way for developers to determine if a tab has focus. This uses the HTML 5 Page Visibility API. Different browsers currently use different implementations, but this guide should help simplify things for you.

Posted in these interests:

javascript
PRIMARY
27 guides
document.hiddendocument.hidden

The first and most simple thing this API provides is a property on the document that lets you know if the page is currently hidden. To run a quick test, open the console on this very page (cmd + option + j on Mac) and type:

document.hidden

As expected this should print:

false

Obviously, the page you are currently viewing is not hidden. But now type this into your console:

setTimeout(function() { console.log(document.hidden); }, 4000);

and immediately switch to another tab. Wait at least four seconds and switch back to this tab. You will now see:

true

This is because the page was hidden when the timeout callback fired.

There are three possible visibility states: hidden, visible, and prerender. hidden and visible are self-explanatory, and prerender is simply the state before the page is fully rendered while the tab is in focus.

This is the event that is fired whenever the browser comes into or goes out of focus.

document.addEventListener('visibilitychange', function(e) { console.log(document.hidden); });

The following code is a modification of what you’ll find at the Mozilla Developer Network documentation for the Page Visibility API.

// Set the name of the hidden property and the change event for visibility var hidden, visibilityChange; if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support hidden = "hidden"; visibilityChange = "visibilitychange"; state = "visibilityState"; } else if (typeof document.mozHidden !== "undefined") { hidden = "mozHidden"; visibilityChange = "mozvisibilitychange"; state = "mozVisibilityState"; } else if (typeof document.msHidden !== "undefined") { hidden = "msHidden"; visibilityChange = "msvisibilitychange"; state = "msVisibilityState"; } else if (typeof document.webkitHidden !== "undefined") { hidden = "webkitHidden"; visibilityChange = "webkitvisibilitychange"; state = "webkitVisibilityState"; }

We need to determine the correct attribute names based on the browser. So utilizing the code above we will access our hidden property like this:

document[hidden]
as opposed to:
document.hidden
This will help keep our code compatible across browsers. Whenever a user changes tabs and the visibility state changes, an event is fired. To listen to the event you can do the following:
document.addEventListener(visibilityChange, function() { console.log(document[hidden]); });

With the code above, you can switch tabs and it will log the state to the console.

Check out our page-visibility module on Github. We will probably expand this over time, but the main purpose is to abstract the code that makes this thing cross browser compatible. So with our module, instead of doing this:

document[hidden]

or

document.hidden

you can do:

Visibility.isHidden()

And you can also get the event name easily:

document.addEventListener(Visibility.visibilitychange, function() { console.log(Visibility.isHidden()); });

And if you want your callback to run only the first time the page becomes visible you can do this:

document.addEventListener(Visibility.visibilitychange, function(e) { var hidden = Visibility.isHidden(); if (!hidden) { console.log('This first only the first time the page is visible!!'); e.target.removeEventListener(e.type, arguments.callee); } });
Jump to step

Determine if a Tab has Focus in JavaScript

Tyler Tyler (285)
Total time: 2 minutes 
Updated: July 22nd, 2020

Tabbed browsing has become the standard so modern browsers have provided a way for developers to determine if a tab has focus. This uses the HTML 5 Page Visibility API. Different browsers currently use different implementations, but this guide should help simplify things for you.

Posted in these interests:

javascript
PRIMARY
27 guides
document.hiddendocument.hidden

The first and most simple thing this API provides is a property on the document that lets you know if the page is currently hidden. To run a quick test, open the console on this very page (cmd + option + j on Mac) and type:

document.hidden

As expected this should print:

false

Obviously, the page you are currently viewing is not hidden. But now type this into your console:

setTimeout(function() { console.log(document.hidden); }, 4000);

and immediately switch to another tab. Wait at least four seconds and switch back to this tab. You will now see:

true

This is because the page was hidden when the timeout callback fired.

There are three possible visibility states: hidden, visible, and prerender. hidden and visible are self-explanatory, and prerender is simply the state before the page is fully rendered while the tab is in focus.

This is the event that is fired whenever the browser comes into or goes out of focus.

document.addEventListener('visibilitychange', function(e) { console.log(document.hidden); });

The following code is a modification of what you’ll find at the Mozilla Developer Network documentation for the Page Visibility API.

// Set the name of the hidden property and the change event for visibility var hidden, visibilityChange; if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support hidden = "hidden"; visibilityChange = "visibilitychange"; state = "visibilityState"; } else if (typeof document.mozHidden !== "undefined") { hidden = "mozHidden"; visibilityChange = "mozvisibilitychange"; state = "mozVisibilityState"; } else if (typeof document.msHidden !== "undefined") { hidden = "msHidden"; visibilityChange = "msvisibilitychange"; state = "msVisibilityState"; } else if (typeof document.webkitHidden !== "undefined") { hidden = "webkitHidden"; visibilityChange = "webkitvisibilitychange"; state = "webkitVisibilityState"; }

We need to determine the correct attribute names based on the browser. So utilizing the code above we will access our hidden property like this:

document[hidden]
as opposed to:
document.hidden
This will help keep our code compatible across browsers. Whenever a user changes tabs and the visibility state changes, an event is fired. To listen to the event you can do the following:
document.addEventListener(visibilityChange, function() { console.log(document[hidden]); });

With the code above, you can switch tabs and it will log the state to the console.

Check out our page-visibility module on Github. We will probably expand this over time, but the main purpose is to abstract the code that makes this thing cross browser compatible. So with our module, instead of doing this:

document[hidden]

or

document.hidden

you can do:

Visibility.isHidden()

And you can also get the event name easily:

document.addEventListener(Visibility.visibilitychange, function() { console.log(Visibility.isHidden()); });

And if you want your callback to run only the first time the page becomes visible you can do this:

document.addEventListener(Visibility.visibilitychange, function(e) { var hidden = Visibility.isHidden(); if (!hidden) { console.log('This first only the first time the page is visible!!'); e.target.removeEventListener(e.type, arguments.callee); } });

Determine if a Tab has Focus in JavaScript

Tyler Tyler (285)
Total time: 2 minutes 
Updated: July 22nd, 2020

Tabbed browsing has become the standard so modern browsers have provided a way for developers to determine if a tab has focus. This uses the HTML 5 Page Visibility API. Different browsers currently use different implementations, but this guide should help simplify things for you.

Posted in these interests:

javascript
PRIMARY
27 guides

Determine if a Tab has Focus in JavaScript

javascript
Tyler Tyler (285)
Total time: 2 minutes 
Updated: July 22nd, 2020
Tyler
 

Posted in these interests:

javascript
PRIMARY
27 guides
javascript
PRIMARY
27 guides
PRIMARY
Jump to step
Calling all writers!

We’re hiring. Write for Howchoo

 
In these interests
javascript
PRIMARY
27 guides
javascript
PRIMARY
27 guides
PRIMARY
Jump to step
document.hiddendocument.hidden

The first and most simple thing this API provides is a property on the document that lets you know if the page is currently hidden. To run a quick test, open the console on this very page (cmd + option + j on Mac) and type:

document.hidden

As expected this should print:

false

Obviously, the page you are currently viewing is not hidden. But now type this into your console:

setTimeout(function() { console.log(document.hidden); }, 4000);

and immediately switch to another tab. Wait at least four seconds and switch back to this tab. You will now see:

true

This is because the page was hidden when the timeout callback fired.

There are three possible visibility states: hidden, visible, and prerender. hidden and visible are self-explanatory, and prerender is simply the state before the page is fully rendered while the tab is in focus.

This is the event that is fired whenever the browser comes into or goes out of focus.

document.addEventListener('visibilitychange', function(e) { console.log(document.hidden); });

The following code is a modification of what you’ll find at the Mozilla Developer Network documentation for the Page Visibility API.

// Set the name of the hidden property and the change event for visibility var hidden, visibilityChange; if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support hidden = "hidden"; visibilityChange = "visibilitychange"; state = "visibilityState"; } else if (typeof document.mozHidden !== "undefined") { hidden = "mozHidden"; visibilityChange = "mozvisibilitychange"; state = "mozVisibilityState"; } else if (typeof document.msHidden !== "undefined") { hidden = "msHidden"; visibilityChange = "msvisibilitychange"; state = "msVisibilityState"; } else if (typeof document.webkitHidden !== "undefined") { hidden = "webkitHidden"; visibilityChange = "webkitvisibilitychange"; state = "webkitVisibilityState"; }

We need to determine the correct attribute names based on the browser. So utilizing the code above we will access our hidden property like this:

document[hidden]
as opposed to:
document.hidden
This will help keep our code compatible across browsers. Whenever a user changes tabs and the visibility state changes, an event is fired. To listen to the event you can do the following:
document.addEventListener(visibilityChange, function() { console.log(document[hidden]); });

With the code above, you can switch tabs and it will log the state to the console.

Check out our page-visibility module on Github. We will probably expand this over time, but the main purpose is to abstract the code that makes this thing cross browser compatible. So with our module, instead of doing this:

document[hidden]

or

document.hidden

you can do:

Visibility.isHidden()

And you can also get the event name easily:

document.addEventListener(Visibility.visibilitychange, function() { console.log(Visibility.isHidden()); });

And if you want your callback to run only the first time the page becomes visible you can do this:

document.addEventListener(Visibility.visibilitychange, function(e) { var hidden = Visibility.isHidden(); if (!hidden) { console.log('This first only the first time the page is visible!!'); e.target.removeEventListener(e.type, arguments.callee); } });
document.hiddendocument.hidden

The first and most simple thing this API provides is a property on the document that lets you know if the page is currently hidden. To run a quick test, open the console on this very page (cmd + option + j on Mac) and type:

document.hidden

As expected this should print:

false

Obviously, the page you are currently viewing is not hidden. But now type this into your console:

setTimeout(function() { console.log(document.hidden); }, 4000);

and immediately switch to another tab. Wait at least four seconds and switch back to this tab. You will now see:

true

This is because the page was hidden when the timeout callback fired.

document.hiddendocument.hidden

The first and most simple thing this API provides is a property on the document that lets you know if the page is currently hidden. To run a quick test, open the console on this very page (cmd + option + j on Mac) and type:

document.hidden

As expected this should print:

false

Obviously, the page you are currently viewing is not hidden. But now type this into your console:

setTimeout(function() { console.log(document.hidden); }, 4000);

and immediately switch to another tab. Wait at least four seconds and switch back to this tab. You will now see:

true

This is because the page was hidden when the timeout callback fired.

document.hidden

There are three possible visibility states: hidden, visible, and prerender. hidden and visible are self-explanatory, and prerender is simply the state before the page is fully rendered while the tab is in focus.

There are three possible visibility states: hidden, visible, and prerender. hidden and visible are self-explanatory, and prerender is simply the state before the page is fully rendered while the tab is in focus.

document.visibilityState

This is the event that is fired whenever the browser comes into or goes out of focus.

document.addEventListener('visibilitychange', function(e) { console.log(document.hidden); });

This is the event that is fired whenever the browser comes into or goes out of focus.

document.addEventListener('visibilitychange', function(e) { console.log(document.hidden); });

visibilitychange event

The following code is a modification of what you’ll find at the Mozilla Developer Network documentation for the Page Visibility API.

// Set the name of the hidden property and the change event for visibility var hidden, visibilityChange; if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support hidden = "hidden"; visibilityChange = "visibilitychange"; state = "visibilityState"; } else if (typeof document.mozHidden !== "undefined") { hidden = "mozHidden"; visibilityChange = "mozvisibilitychange"; state = "mozVisibilityState"; } else if (typeof document.msHidden !== "undefined") { hidden = "msHidden"; visibilityChange = "msvisibilitychange"; state = "msVisibilityState"; } else if (typeof document.webkitHidden !== "undefined") { hidden = "webkitHidden"; visibilityChange = "webkitvisibilitychange"; state = "webkitVisibilityState"; }

We need to determine the correct attribute names based on the browser. So utilizing the code above we will access our hidden property like this:

document[hidden]
as opposed to:
document.hidden
This will help keep our code compatible across browsers. Whenever a user changes tabs and the visibility state changes, an event is fired. To listen to the event you can do the following:
document.addEventListener(visibilityChange, function() { console.log(document[hidden]); });

With the code above, you can switch tabs and it will log the state to the console.

The following code is a modification of what you’ll find at the Mozilla Developer Network documentation for the Page Visibility API.

// Set the name of the hidden property and the change event for visibility var hidden, visibilityChange; if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support hidden = "hidden"; visibilityChange = "visibilitychange"; state = "visibilityState"; } else if (typeof document.mozHidden !== "undefined") { hidden = "mozHidden"; visibilityChange = "mozvisibilitychange"; state = "mozVisibilityState"; } else if (typeof document.msHidden !== "undefined") { hidden = "msHidden"; visibilityChange = "msvisibilitychange"; state = "msVisibilityState"; } else if (typeof document.webkitHidden !== "undefined") { hidden = "webkitHidden"; visibilityChange = "webkitvisibilitychange"; state = "webkitVisibilityState"; }

We need to determine the correct attribute names based on the browser. So utilizing the code above we will access our hidden property like this:

document[hidden]
as opposed to:
document.hidden
This will help keep our code compatible across browsers. Whenever a user changes tabs and the visibility state changes, an event is fired. To listen to the event you can do the following:
document.addEventListener(visibilityChange, function() { console.log(document[hidden]); });

With the code above, you can switch tabs and it will log the state to the console.

Cross browser compatible implementation

Check out our page-visibility module on Github. We will probably expand this over time, but the main purpose is to abstract the code that makes this thing cross browser compatible. So with our module, instead of doing this:

document[hidden]

or

document.hidden

you can do:

Visibility.isHidden()

And you can also get the event name easily:

document.addEventListener(Visibility.visibilitychange, function() { console.log(Visibility.isHidden()); });

And if you want your callback to run only the first time the page becomes visible you can do this:

document.addEventListener(Visibility.visibilitychange, function(e) { var hidden = Visibility.isHidden(); if (!hidden) { console.log('This first only the first time the page is visible!!'); e.target.removeEventListener(e.type, arguments.callee); } });

Check out our page-visibility module on Github. We will probably expand this over time, but the main purpose is to abstract the code that makes this thing cross browser compatible. So with our module, instead of doing this:

document[hidden]

or

document.hidden

you can do:

Visibility.isHidden()

And you can also get the event name easily:

document.addEventListener(Visibility.visibilitychange, function() { console.log(Visibility.isHidden()); });

And if you want your callback to run only the first time the page becomes visible you can do this:

document.addEventListener(Visibility.visibilitychange, function(e) { var hidden = Visibility.isHidden(); if (!hidden) { console.log('This first only the first time the page is visible!!'); e.target.removeEventListener(e.type, arguments.callee); } });

Use our page visibility module

Calling all writers!

We’re hiring. Write for Howchoo

Tyler's profile pictureTyler
Joined in 2015
Software Engineer and creator of howchoo.
Tyler's profile picture
Share this guide!
RedditEmailText
Related to this guide:
How to Pretty Print JSON in Chrome Developer ConsoleHow to Pretty Print JSON in Chrome Developer Console
In these interests: codejavascriptwebdev
Remove Elements From an Array in JavaScriptRemove Elements From an Array in JavaScript
When working with arrays in JavaScript, we often need to remove elements.
Tyler's profile picture TylerView
In these interests: codejavascript
Enumerable Properties in JavaScriptEnumerable Properties in JavaScript
Enumerable properties are properties whose internal enumerable flag set to true.
Tyler's profile picture TylerView
In these interests: codejavascript
How to Pretty Print JSON in Chrome Developer ConsoleHow to Pretty Print JSON in Chrome Developer Console
In these interests: codejavascriptwebdev
Zach's profile pictureViewcodejavascriptwebdev
Remove Elements From an Array in JavaScriptRemove Elements From an Array in JavaScript
When working with arrays in JavaScript, we often need to remove elements.
Tyler's profile picture TylerView
In these interests: codejavascript
Tyler's profile pictureViewcodejavascript
Enumerable Properties in JavaScriptEnumerable Properties in JavaScript
Enumerable properties are properties whose internal enumerable flag set to true.
Tyler's profile picture TylerView
In these interests: codejavascript
Tyler's profile pictureViewcodejavascript
People also read:
This guide demonstrates multiple ways to iterate over a JavaScript object’s properties and values.
There are two very similar statements in JavaScript: for…in and for…of. And while they can be easily confused, they’re actually quite different.
My journey trying to find the one loop operator to rule them all
As a JavaScript developer, you’ll often need to construct URLs and query string parameters. One sensible way to construct query string parameters is to use a one layer object with key value pairs.
Writing tests is an important part of software development process. Unit tests form a core part of testing process where each functional block is tested as an independent unit.
Making a deep copy of an object in JavaScript is fairly challenging. Fortunately, there are a few ways to accomplish this without much code.
At some point you’ll want to read, set, and remove cookies using JavaScript.
Learn how to split a string into an array.
This guide will teach you how to concatenate, or join, all elements of an array into a single string.
Learn how to merge two arrays together in JavaScript.
This guide demonstrates multiple ways to iterate over a JavaScript object’s properties and values.
There are two very similar statements in JavaScript: for…in and for…of. And while they can be easily confused, they’re actually quite different.
My journey trying to find the one loop operator to rule them all
As a JavaScript developer, you’ll often need to construct URLs and query string parameters. One sensible way to construct query string parameters is to use a one layer object with key value pairs.
Writing tests is an important part of software development process. Unit tests form a core part of testing process where each functional block is tested as an independent unit.
Learn Multiple Ways to Iterate Over JavaScript Object Properties and Values
The Difference Between “for…in” and “for…of” in JavaScript
JavaScript for loops
How to Turn an Object into Query String Parameters in JavaScript
Unit Testing in JavaScript – Mocha, Chai and Sinon – a Beginner’s Guide
Making a deep copy of an object in JavaScript is fairly challenging. Fortunately, there are a few ways to accomplish this without much code.
At some point you’ll want to read, set, and remove cookies using JavaScript.
Learn how to split a string into an array.
This guide will teach you how to concatenate, or join, all elements of an array into a single string.
Learn how to merge two arrays together in JavaScript.
How to Copy an Object in JavaScript
How to Manage Cookies in JavaScript
How to Split a String into an Array in JavaScript
How to Join All Elements of an Array in JavaScript
How to Merge Two Arrays in JavaScript
Posted in these interests:
javascriptjavascript
javascript
PRIMARY
Array(16).join(“wat” – 1) + ” Batman!”;
javascriptjavascript
javascript
PRIMARY
Array(16).join(“wat” – 1) + ” Batman!”;
PRIMARY
Explore
Discuss this guide:
We’re hiring!
Are you a passionate writer? We want to hear from you!
We’re hiring!
Are you a passionate writer? We want to hear from you!
View openings

Want to support Howchoo? When you buy a tool or material through one of our Amazon links, we earn a small commission as an Amazon Associate.

Donate

Leave a Reply

Your email address will not be published. Required fields are marked *