Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery: Get favicon with full path

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.

like image 608
Paul Fournel Avatar asked Mar 05 '13 09:03

Paul Fournel


1 Answers

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;
like image 115
David Thomas Avatar answered Nov 15 '22 04:11

David Thomas