views:

202

answers:

4

Hi,

I've seen lots on how to do this with an 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?

Thanks,
Matt

A: 

Try alert($("link").attr('href'));

Fogh
He already tried that: If $("link").length = 0, then your answer will certainly return "undefined".
Prutswonder
I was able to select a link tag with jquery. Is the document ready?
enduro
+2  A: 

If $("link").length is returning 0, your selector isn't coming up with anything and there's no hope for getting the link at all.

You need a better selector. Maybe attack it by ID (#linkId) or by a particular class, etc. There's tons of selectors you can use:

http://api.jquery.com/category/selectors/

Your first step is to get the .length to show 1 though. Then from there .attr('href') will give you the link location.

Edit: Sorry, misinterpreted what you are going for. I was thinking you meant the anchor tag. Here's how I've successfully accessed a link tag:

var links = window.document.getElementsByTagName('link');
$(links).each(function() {
    $(this).attr('href')  // this is your href for the link tag in the loop
});
macca1
I actually have the html contained within a var... I've pulled a page from another site and I'm trying to pull an href out of the link tags. When I alert the variable it shows clearly that there are link tags but if I do either: html.getElementsByTagName('link').length or $("link", html).length both give 0...
Matt
I'm confused. window.document.getElementsByTagName('link') returns an array of several items (one for each <link> in the document <head>) But then my anonymous function does not get called at all (judging by adding console.log('X') into the function, or by adding breakpoints to it)
Tartley
@Tartley: This is assuming you are using jQuery for the .each() function. Are you using jQuery?
macca1
Hey, thanks for the response macca1. I did have jQuery referenced by a script tag in my document, but I'm not sure what else I needed to do. Don't fret though, I gave up and did it some totally other way, so I'm no longer worried about it. My javascript/jQuery education will have to wait for another day.
Tartley
+2  A: 

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"&gt;&lt;/script&gt; 
<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?

Prutswonder
A: 

UPDATE

try this way!

<link rel="stylesheet" href="style.css" type="text/css">

$('link[@rel*=style]').each(function(i) { 
alert( $(this).length );
});
aSeptik