Created
February 14, 2023 23:51
-
-
Save roman-bgonz/01d32fee74aa1d63aad9897ccfed6046 to your computer and use it in GitHub Desktop.
Changes title to tab when user changes to another tab
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | |
<title>Page Title</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1" /> | |
<link rel="stylesheet" type="text/css" media="screen" href="main.css" /> | |
<script> | |
// Our current page title | |
let previousTitle = document.title; | |
// We are going to list the BLUR event from window | |
window.addEventListener('blur', () => { | |
// When the user changes from tab, we change the title | |
previousTitle = document.title; | |
document.title = "Please Don't Go"; | |
}); | |
// When tab is focused we return to the original title | |
window.addEventListener('focus', () => { | |
document.title = previousTitle; | |
}); | |
</script> | |
</head> | |
<body></body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment