Skip to content

Instantly share code, notes, and snippets.

@nickberens360
Last active August 27, 2015 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickberens360/0d6b18fcb526d0532754 to your computer and use it in GitHub Desktop.
Save nickberens360/0d6b18fcb526d0532754 to your computer and use it in GitHub Desktop.
jQuery: Change meta title tag when page is not being viewed
//Code found here: https://graphicfusion.design/
jQuery(document).ready(function($) {
(function() {
var hidden = "hidden";
var oldtitle = document.title;
var currenttitle;
if (hidden in document)
document.addEventListener("visibilitychange", onchange);
else if ((hidden = "mozHidden") in document)
document.addEventListener("mozvisibilitychange", onchange);
else if ((hidden = "webkitHidden") in document)
document.addEventListener("webkitvisibilitychange", onchange);
else if ((hidden = "msHidden") in document)
document.addEventListener("msvisibilitychange", onchange);
else if ("onfocusin" in document)
document.onfocusin = document.onfocusout = onchange;
else
window.onpageshow = window.onpagehide = window.onfocus = window.onblur = onchange;
function onchange(evt) {
var v = "visible"
, h = "hidden"
, evtMap = {
focus: v,
focusin: v,
pageshow: v,
blur: h,
focusout: h,
pagehide: h
};
evt = evt || window.event;
if (evt.type in evtMap) {
currenttitle = oldtitle;
$(document).attr('title', currenttitle);
}
else {
currenttitle = this[hidden] ? "Miss You ︎:( " : oldtitle;
$(document).attr('title', currenttitle);
}
}
if (document[hidden] !== undefined)
onchange({
type: document[hidden] ? "blur" : "focus"
});
}
)();
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment