我有以下(jquery)选择器寻找外部链接。如何使用JavaScript匹配外部链接(但忽略子域名)?
这工作,而忽略包括所有链接location.hostname
(例如www.domain.com)
我的问题是,如何延长这也忽略链接到你的网站的子域? (例如new.domain.com)
$('a').each(function() {
var href = $(this).attr('href');
if(this.hostname && this.hostname !== location.hostname)
{
$(this)
.removeAttr('target')
.attr('rel', 'external')
.attr('title', href)
.click(function() {
window.open($(this).attr('href'));
return false;
});
}
});
您还可能需要验证零件数组的长度。可能是这个域只包含'domain.com'。 – cmptrgeekken 2010-02-24 15:03:53
是的,就像我说的那样,这取决于相关域的设置。制作适用于任何域/子域系列的脚本可能相当困难。 – Pointy 2010-02-24 15:18:43