结合2个bookmaklets

时间:2013-02-17 00:28:56

标签: javascript bookmarklet

我有2个单独的书签

javascript: (function () { var url = document.location; var links = document.getElementsByTagName('link'); var myvar = 'none'; for (var i = 0, l; l = links[i]; i++) { if (l.getAttribute('rel') == 'next') { myvar = l.getAttribute('href'); break; } } alert(myvar); })();

javascript: (function () { var url = document.location; var links = document.getElementsByTagName('link'); var myvar = 'none'; for (var i = 0, l; l = links[i]; i++) { if (l.getAttribute('rel') == 'prev') { myvar = l.getAttribute('href'); break; } } alert(myvar); })();

我希望将它们组合成一个,这样两个都显示在一个书签中。我希望这会做到,但显然我已经塞满了

javascript: (function () { var url = document.location; var links = document.getElementsByTagName('link'); var myvar = 'none'; var myvar1 = 'none'; for (var i = 0, l; l = links[i]; i++) { if (l.getAttribute('rel') == 'next') { myvar = l.getAttribute('href'); break; } } { if (l.getAttribute('rel') == 'prev') { myvar1 = l.getAttribute('href'); break; } } alert(myvar + ' ' + myvar1); })();

1 个答案:

答案 0 :(得分:0)

试试这个:

javascript: (function () { var url = document.location; var links = document.getElementsByTagName('link'); var prev = 'none', next = 'none'; for (var i = 0, l; l = links[i]; i++) { if (l.getAttribute('rel') == 'prev') { prev = l.getAttribute('href'); } else if(l.getAttribute('rel') == 'next') { next = l.getAttribute('href'); } if(prev != 'none' && next != 'none') { break; }} alert(prev + '\n' + next); })();

此外,这是一个缩小版本:

javascript:(function(){for(var e=document.getElementsByTagName("link"),b="none",c="none",d=0,a;(a=e[d])&&!("prev"==a.getAttribute("rel")?b=a.getAttribute("href"):"next"==a.getAttribute("rel")&&(c=a.getAttribute("href")),"none"!=b&&"none"!=c);d++);alert(b+"\n"+c)})();