最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to get the url from link rel="next" in <head> with javascript - Stack Overflow

programmeradmin0浏览0评论

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
Add a comment  | 

3 Answers 3

Reset to default 16

IE8+

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;
    }
}
发布评论

评论列表(0)

  1. 暂无评论