Created
January 6, 2024 19:33
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
var isTabActive; | |
window.onfocus = function () { | |
console.log("onfocus"); | |
}; | |
window.onblur = function () { | |
console.log("onblue"); | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.