我正在使用一个简单的 Jquery 脚本在所有外部链接上强制使用 target="_blank"
。问题是它会在新窗口中打开子域。我想调整此代码,使其允许在同一浏览器 session 中使用子域,而不是强制打开新窗口。
例如,如果我的网站在 http://pixeltest.com 上我有一个链接到http://test.pixeltest.com , 将在新窗口中打开。
代码:
$("a").filter(function() {
return this.hostname && this.hostname !== location.hostname;
}).attr('target', '_blank');
关于如何做到这一点有什么想法吗?
最佳答案
改变
return this.hostname && this.hostname !== location.hostname;
到
return this.hostname && this.hostname.substr(this.hostname.indexOf('.')) !== location.hostname.substr(location.hostname.indexOf('.'));
这应该只会比较第一个点之后的所有内容。
关于javascript - 创建在新窗口打开外部链接的异常(exception),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12201339/