Jquery: Get favicon with full path

早过忘川 提交于 2019-12-21 19:42:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!