问题
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.
回答1:
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;
来源:https://stackoverflow.com/questions/15220412/jquery-get-favicon-with-full-path