I want to grab the link of a favicon from a website with Jquery. I simply use $('link[rel="shortcut icon"]').attr('href');
and it works fine.
But... I do not always have a complete path. For example I can have something like
http://mywebsite.com/myicon.ico
or only a relative path
/myicon.ico
I would like to always have the full path, even if the Url is coded as a relative path. Is there a simple solution to this?
I use this as a Content Script in a Google Chrome Extension.
if you use the href
property of the node, you should always get an absolute path:
var url = $('link[rel="shortcut icon"]')[0].href;
Or:
var url = $('link[rel="shortcut icon"]').prop('href');
Or, in plain JavaScript:
var url = document.querySelector('link[rel="shortcut icon"]').href;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With