javascript - 在新标签页中访问网站时,网站如何重定向现有标签页

标签 javascript html redirect

我正在尝试打开来自 Course Report 的页面在新标签页中。通常我会这样做:

<a href="https://www.coursereport.com/schools/flatiron-school" target="_blank">https://www.coursereport.com/schools/flatiron-school</a>

然而,类(class)报告正在做一些奇怪的事情。如果我使用该链接,它:

  • 短暂正确地打开标签页
  • 标签页关闭
  • 原始标签(我点击链接的地方)重定向到类(class)报告

我感兴趣

  • 类(class)报告如何实现这一点(新选项卡可以对引用选项卡进行如此多的控制似乎很糟糕)
  • 我可以做些什么来防止这种情况发生并获得默认行为。

我似乎无法在 stackoverflow 中直接重现,但这是一个 HackMD 文档,您可以在其中看到行为 https://hackmd.io/s/Hy1Ln7g8X

最佳答案

重现行为

因此您可以通过创建 2 个文件来重现该行为:

1.html

<a href="2.html" target="_blank">Go to second page</a>

2.html

<script>
window.opener.location="about:blank";
window.close();
</script>

从目标页面重写源标签可以作为reverse tabnabbing这可能很危险。

Reverse tabnabbing is an attack where a page linked from the target page is able to rewrite that page, for example to replace it with a phishing site. As the user was originally on the correct page they are less likely to notice that it has been changed to a phishing site, especially it the site looks the same as the target.

防止行为

正如 window.opener 的文档所说,您可以使用属性阻止这种行为,但这仅适用于某些浏览器(Firefox 52+、Chrome 49+ , Opera 36+, Safari 10.1+):

In some browsers, a rel="noopener" attribute on the originating anchor tag will prevent the window.opener reference from being set.

<a href="2.html" target="_blank" rel="noopener">Go to second page</a>

我还找到了this page关于 rel=noopener 也提到了针对旧浏览器的可能解决方案:

For older browsers, you could use rel=noreferrer which also disables the Referer HTTP header, or the following JavaScript work-around which potentially triggers the popup blocker:

var otherWindow = window.open();
otherWindow.opener = null;
otherWindow.location = url;

您可以组合 rel 属性的值,例如 rel="noreferrer noopener"

关于javascript - 在新标签页中访问网站时,网站如何重定向现有标签页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51839302/

相关文章:

javascript - 我无法使用 sequelize Node 在 MySQL 中设置本地 "timezone"

javascript - Onclick div 从左到右移动,在窗口点击返回到它的原始位置

php - html 文本区域显示默认或最近的输入

css - Node.js 重定向 CSS 302 暂时移动

JavaScript 生成器风格的异步

javascript - 为什么Apache服务器每天都会自动启动并且它会停止nodeJs?

html - 在标准表单布局模式下使用 Twitter Bootstrap 时如何限制控件宽度?

JQuery 显示/隐藏慢点击事件

java - 如何重定向到不同 EAR 中的登录入口点

java - JSF 移动/重定向到其他包含数据的页面