如何不自动关注所有外部链接 - jQuery

时间:2015-01-23 13:04:51

标签: javascript jquery html seo nofollow

我想在所有外部链接中添加rel="nofollow"属性。以下代码适用于http://https://附带的链接。我的问题是它在没有http的链接上没有用(例如:/somepage.html)我怎么能超过这个。

高度赞赏任何解决方案。

JS Fiddle

HTML

<a href="http://www.somepage.com">This is an internal link</a>
<br /><br />
<a href="http://google.com">And this is an external link, with no follow (working)</a>
<br />
<br />
<a href="google.com">And this is an external link, with no follow (not-working)</a>
<br />
<br />
<a href="https://plus.google.com/101147114025225446015/about">And this is an external link, with no follow (working)</a>

JS

jQuery('a[href*="http://"]:not([href*="http://www.somepage.com"])').attr('rel', 'nofollow');

jQuery('a[href*="https://"]:not([href*="http://www.somepage.com"])').attr('rel', 'nofollow');

jQuery('a[href*="https://"]:not([href*="https://www.somepage.com"])').attr("target", "_blank");

JS Fiddle

0 个答案:

没有答案