Skip to content

Instantly share code, notes, and snippets.

@kezabelle
Created November 5, 2014 08:55
Show Gist options
  • Save kezabelle/8e3068cee9b5173e43bf to your computer and use it in GitHub Desktop.
Save kezabelle/8e3068cee9b5173e43bf to your computer and use it in GitHub Desktop.
Changing a tab/window's document title when it isn't in focus. Supports modern browsers, in theory. Does nothing in older ones, hopefully.
/*
if the browser supports it, and the page isn't in focus (tabbed away, etc)
we change the title to whatever is passed into the anonymous function,
as a reminder.
*/
(function(hidden_title, undefined) {
var has_document;
var has_title;
var has_event_handler;
var original_title;
has_document = window && window.document !== null;
if (has_document === true) {
has_title = window.document.title !== null;
if (has_title === true) {
original_title = window.document.title;
has_event_handler = document.addEventListener !== null;
if (has_event_handler === true) {
var change_title = function() {
var is_hidden = window.document.hidden !== null && window.document.hidden === true;
if (is_hidden) {
if (hidden_title !== void(0)) {
window.document.title = hidden_title;
}
} else {
window.document.title = original_title;
}
};
document.addEventListener("visibilitychange", change_title, false);
}
}
}
})('MY AWESOME TITLE YO');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment