Skip to content

Instantly share code, notes, and snippets.

@mudassaralichouhan
Created January 6, 2024 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mudassaralichouhan/f2b976a5dcdfa99b231f7de8c1131d9f to your computer and use it in GitHub Desktop.
Save mudassaralichouhan/f2b976a5dcdfa99b231f7de8c1131d9f to your computer and use it in GitHub Desktop.
This approach can be used to track if a user has left a webpage by switching to another browser tab or application.
<script>
var isTabActive;
window.onfocus = function () {
console.log("onfocus");
};
window.onblur = function () {
console.log("onblue");
};
</script>
@mudassaralichouhan
Copy link
Author

In this script, isTabActive is a boolean variable to store the current active tab status.

When the browser window receives focus (window.onfocus), the variable is set to true. Similarly, when the window loses focus (window.onblur), the variable is set to false.

This approach can be used to track if a user has left a webpage by switching to another browser tab or application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment