javascript - 创建在新窗口打开外部链接的异常(exception)

标签 javascript jquery hostname

我正在使用一个简单的 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/

相关文章:

javascript - 保持向右浮动内容的高度与向左浮动的兄弟 div 相同,响应式

javascript - chrome 扩展 - 使用内容脚本减少加载时间的最佳方法

javascript - 触发具有 $this 的 Jquery 函数。加载和单击

javascript - jQuery 日期/时间选择器未通过 .serialize() 序列化)

javascript - 谷歌浏览器扩展,一键删除浏览历史

javascript - 在 div 上制作三 Angular 形

javascript - ng-click 事件似乎不会在简单的 Angular 应用程序中触发

Intellij IDEA 中的 Maven 无法解析符号 env.HOSTNAME

powershell - 在Powershell中使用主机名作为变量创建if语句时出错

amazon-ec2 - 在 Kubernetes 上运行时更改主机名会破坏 Rabbitmq