Skip to content

Instantly share code, notes, and snippets.

@mohammadwali
Created September 27, 2015 22:35
Show Gist options
  • Save mohammadwali/a426c9eb16ffecf2e775 to your computer and use it in GitHub Desktop.
Save mohammadwali/a426c9eb16ffecf2e775 to your computer and use it in GitHub Desktop.
Change Browsers Favicon Dinamically using JavaScript (Credits: http://softwareas.com/dynamic-favicons/)
var favicon = {
change: function(iconURL) {
if (arguments.length == 2) {
document.title = optionalDocTitle
}
this.addLink(iconURL, "icon")
this.addLink(iconURL, "shortcut icon")
// Google Chrome HACK - whenever an IFrame changes location
// (even to about:blank), it updates the favicon for some reason
// It doesn't work on Safari at all though :-(
if (!IE) { // Disable the IE "click" sound
if (!window.__IFrame) {
__IFrame = document.createElement('iframe')
var s = __IFrame.style
s.height = s.width = s.left = s.top = s.border = 0
s.position = 'absolute'
s.visibility = 'hidden'
document.body.appendChild(__IFrame)
}
__IFrame.src = 'about:blank'
}
},
addLink: function(iconURL, relValue) {
var link = document.createElement("link")
link.type = "image/x-icon"
link.rel = relValue
link.href = iconURL
this.removeLinkIfExists(relValue)
this.docHead.appendChild(link)
},
removeLinkIfExists: function(relValue) {
var links = this.docHead.getElementsByTagName("link");
for (var i = 0; i < links.length; i++) {
var link = links[i]
if (link.type == "image/x-icon" && link.rel == relValue) {
this.docHead.removeChild(link)
return
}
}
}, // Assuming only one match at most.
docHead: document.getElementsByTagName("head")[0]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment