Change Page Title on Leave
Swaps the browser tab title to a custom message when the user switches away from the tab, and restores the original title when they return.
JavaScriptBrowser APIEngagement
Code
script.js
javascript
const documentTitleStore = document.title;
const documentTitleOnBlur = "Come back! We miss you"; // Define your custom title here
// Restore original title when user returns to the tab
window.addEventListener("focus", () => {
document.title = documentTitleStore;
});
// Show alternative title when user leaves the tab
window.addEventListener("blur", () => {
document.title = documentTitleOnBlur;
});Notes
- •`documentTitleStore` captures `document.title` at script-load time, so the restored title always reflects whatever the page set initially.
- •Change `documentTitleOnBlur` to any string you like — emoji work well here (e.g. `"👋 Come back!"`) for extra visibility in the tab bar.
- •The `focus`/`blur` events fire on the `window` object and trigger whenever the tab loses or regains focus, including when the user switches to another application entirely.