我想用Jquery从网站上抓取图标的链接。我简单地使用 $('link [rel =快捷方式图标]')。attr('href'); ,它工作正常。
但是......我并不总是有一条完整的路径。例如,我可以拥有类似于
mywebsite/myicon.ico 或者只有一个相对路径 /myicon.ico我希望始终拥有完整路径,即使Url编码为一个相对路径。是否有简单的解决方案?
我将它用作Google Chrome扩展程序中的内容脚本。
'DIV CLASS = h2_lin>解决方案如果您使用 HREF节点属性,你应该总是得到一个绝对路径:
var url = $('link [rel =shortcut icon]')[0] .href;或者:
var url = $('link [rel =shortcut icon]')。prop('href');或者,在普通JavaScript中:
var url = document.querySelector('link [rel =shortcut icon]')。href;
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
mywebsite/myicon.icoor only a relative path
/myicon.icoI 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;
更多推荐
Jquery:获取完整路径的图标
发布评论