2

我正在使用一个简单的 Jquery 脚本来强制target="_blank"所有外部链接。问题是它会在新窗口中打开子域。我想调整这段代码,以便它允许在同一个浏览器会话中使用子域,而不是强制一个新窗口。

例如,如果我的网站位于http://pixeltest.com并且我有一个指向http://test.pixeltest.com的链接,那么它将在新窗口中打开。

编码:

$("a").filter(function() {
    return this.hostname && this.hostname !== location.hostname;
}).attr('target', '_blank');

关于我如何做到这一点的任何想法?

4

1 回答 1

2

改变

return this.hostname && this.hostname !== location.hostname;

return this.hostname && this.hostname.substr(this.hostname.indexOf('.')) !== location.hostname.substr(location.hostname.indexOf('.'));

这应该只比较第一个点之后的所有内容。

于 2012-08-30T16:29:12.850 回答