Created
January 14, 2017 22:22
-
-
Save thomasvaeth/a940de5a51ba8202181c76ec896b7c0b to your computer and use it in GitHub Desktop.
Snippet for Title/Favicon Change
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
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