detect if page is active in javascript

Detect if page is active in JavaScript

Why?

The Page Visibility API assists you in detecting when a web page is visible. It could be useful when your website is using one of many browser tabs in a users browser window.

When?

You can use the Page Visibility API to do certain things when a user navigates to a different tab.

  • Pause video and audio 🎵
  • Pause animations 🎞
  • Stop any network requests being made 📞
  • Any other things you might want to do when the user navigates away to another tab. For example, you might change the
  • document.title or play a sound 🔈

JavaScript Code

const handleChange = (e) => {
  document.title = document.hidden ? 'Come back' : 'Welcome Back'
}

document.addEventListener("visibilitychange", handleChange);

Related Posts

remove-duplicate-elements-in-a-list-with-python
Remove duplicate elements in a list with Python

A simple and efficient way to remove duplicate elements from a list using...

Use Emoji’s in Python

Did you know you can use Emoji's in Python? Learn how to!

Please Install Theme Required & Recommended PLugins.