Bry*_*rty 7 javascript hyperlink target window.opener
所以我知道我可以rel="noopener在使用a时使用标签target="_blank".但我试图将其作为参数传递给window.open(),即:
window.open('http://cats.com', '_blank', 'rel=noopener')
但是它似乎没有按照我预期的方式工作,因为opener在用户点击链接后对象仍然存在于窗口中.
有什么我想念的吗?或者不能以我想要的方式完成?
我发现了一些很棒的文章,但就我所知,它们并没有完全解决我的用例.
https://developer.mozilla.org/en-US/docs/Web/API/Window/open https://mathiasbynens.github.io/rel-noopener/
非常感激.
小智 6
据我所知,这不可能通过window.open()争论来实现。但是,有一种方法可以解决此问题:
var newWindow = window.open();
newWindow.opener = null;
newWindow.location = 'http://some.url';
Run Code Online (Sandbox Code Playgroud)
这为我工作:
const a = document.createElement("a")
a.href = args.url
a.target = "_blank"
a.rel = "noopener"
a.click()
Run Code Online (Sandbox Code Playgroud)
在doc中没有直接的示例,但是可以像这样使用,并且对我有用。
window.open('http://cats.com', '_blank', 'noopener,resizable,scrollbars')
Run Code Online (Sandbox Code Playgroud)
重定向最安全的方式是通过添加noopener,noreferrer和window.opener = null。
const openInNewTab = (url) => {
const newWindow = window.open(url, '_blank', 'noopener,noreferrer')
if (newWindow) newWindow.opener = null
}
Run Code Online (Sandbox Code Playgroud)
然后调用你的新函数
openInNewTab('https://stackoverflow.com')
Run Code Online (Sandbox Code Playgroud)
根据您的需要,第三个参数也可以采用这些可选值。
| 归档时间: |
|
| 查看次数: |
2896 次 |
| 最近记录: |