How to get the url from
<link rel="prev" title="Selected 3" href=".html" />
in the head using pure javascript?
In jquery I have a working solution:
var prevUrl = $('link[rel=prev]').attr("href");
I can't change the output of the "link rel" or add an Id, as it is generated by a CMS.
Any help is greatly appreciated, thanks.
How to get the url from
<link rel="prev" title="Selected 3" href="http://mydomain.com/2.html" />
in the head using pure javascript?
In jquery I have a working solution:
var prevUrl = $('link[rel=prev]').attr("href");
I can't change the output of the "link rel" or add an Id, as it is generated by a CMS.
Any help is greatly appreciated, thanks.
Share Improve this question asked Jun 5, 2012 at 21:47 JesJes 731 silver badge3 bronze badges 3- What's the reason why you need it done in pure js? – h2ooooooo Commented Jun 5, 2012 at 21:49
- Well, I'm trying to implement padilicious.com to let a swipe on an iOS device, go to the next/prev page. I use jQuery so one can use the arrow keys to that. But it seems padilicious.com uses only javascript. – Jes Commented Jun 5, 2012 at 21:52
- Sorry link is: padilicious.com/code/touchevents – Jes Commented Jun 5, 2012 at 21:58
3 Answers
Reset to default 16IE8+
document.querySelector('link[rel="prev"]').href;
var links = document.getElementsByTagName( "link" ),
filtered = [],
i = links.length;
while ( i-- ) {
links[i].rel === "prev" && filtered.push( links[i] );
}
alert( filtered[0].href );
Demo: http://jsfiddle.net/je7Qr/
var links = document.getElementsByTagName("link");
for (var i = 0; typeof(el = links[i]) != "undefined"; i++) {
if (el.rel == "prev") {
alert(el.href);
break;
}
}