Skip to content

Instantly share code, notes, and snippets.

@roman-bgonz
Created February 14, 2023 23:51
Show Gist options
  • Save roman-bgonz/01d32fee74aa1d63aad9897ccfed6046 to your computer and use it in GitHub Desktop.
Save roman-bgonz/01d32fee74aa1d63aad9897ccfed6046 to your computer and use it in GitHub Desktop.
Changes title to tab when user changes to another tab
<!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