'How can I get the href of a <link> tag using jQuery?

I've seen lots on how to do this with an <a> tag, and I have no problem doing it with that one, but I can't seem to retrieve the href attribute of a <link> tag.

Even trying to grab the link tag at all:

alert($("link").length);

Gives 0.

Any ideas?



Solution 1:[1]

My guess is that for some reason the link tag isn't available when you execute your alert statement. When I try the following sample:

<html>
<head>
<link rel="stylesheet src="xxx" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 
<script type="text/javascript">
    $(document).ready(function(){
        alert($("link").length);
    });
</script>
</head>
<body>
Hello world!
</body>
</html>

It neatly returns "1", not "0". Maybe you could try to figure out the differences between your code and this example. For instance: Are you running your code from the HEAD or from the BODY tag?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Prutswonder