Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get attribute from a <link tag in pure javascript?

Tags:

javascript

<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://www.blogger.com/rsd.g?blogID=7487871339000666216" />

How can I get the href attribute of this link tag without using any javascript library?

Right now I'm using the code below to achieve this but I want to know whether there is a simpler solution.

var links = document.getElementsByTagName("link");
for (i in links){
    var title = links[i].getAttribute("title");
    if ( title == "RSD"){
        var href = links[i].getAttribute("href");
        break;
    }
}
like image 679
WTFIsGoingOn Avatar asked Feb 25 '12 06:02

WTFIsGoingOn


1 Answers

Modern browsers support querySelector() and querySelectorAll():

document.querySelector("link[title=RSD]").getAttribute("href");

See browser support chart.

like image 183
jholster Avatar answered Sep 18 '22 08:09

jholster