Skip to content

Instantly share code, notes, and snippets.

@thomasvaeth
Created January 14, 2017 22:22
Show Gist options
  • Save thomasvaeth/a940de5a51ba8202181c76ec896b7c0b to your computer and use it in GitHub Desktop.
Save thomasvaeth/a940de5a51ba8202181c76ec896b7c0b to your computer and use it in GitHub Desktop.
Snippet for Title/Favicon Change
var PleaseDontGo = (function() {
var s;
return {
settings: {
originalTitle: document.title,
// New title when tab is changed
newTitle: 'Please Don\'t Go',
favicon: document.querySelectorAll('[rel="icon"]')[0],
originalFavicon: document.querySelectorAll('[rel="icon"]')[0].href,
// New favicon when tab is changed
newFavicon: '/assets/images/favicon-dontgo.ico'
},
init: function() {
s = this.settings;
this.visibility();
},
visibility: function() {
document.addEventListener('visibilitychange', function() {
if (document.visibilityState === 'hidden') {
setTimeout(function() {
document.title = s.newTitle;
s.favicon.setAttribute('href', s.newFavicon);
}, 1500);
} else {
document.title = s.originalTitle;
s.favicon.setAttribute('href', s.originalFavicon);
}
});
}
}
})();
document.addEventListener('DOMContentLoaded', function() {
PleaseDontGo.init();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment