Changing your site's favicon with Javascript

function change_favicon(img) { var favicon = document.querySelector('link[rel="shortcut icon"]'); if (!favicon) { favicon = document.createElement('link'); favicon.setAttribute('rel', 'shortcut icon'); var head = document.querySelector('head'); head.appendChild(favicon); } favicon.setAttribute('type', 'image/png'); favicon.setAttribute('href', img); } change_favicon('https://codepad.co/img/icn_logo.png');
Little function to change your site's favicon problematically to any png.

img variable must be a string, and img must be a png.

If you do not already have a link tag created the function will create one for you and put it in the head section of your site.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.