简体   繁体   English

JavaScript:如何在没有 window.open() 的情况下在新选项卡中打开链接?

[英]JavaScript: How to open a link in a new tab without window.open()?

I have an href that I need to open in a new tab, and can't use an anchor tag because it's from within a webgl context.我有一个 href 需要在新选项卡中打开,并且不能使用锚标记,因为它来自 webgl 上下文。 The following accomplishes this:以下实现了这一点:

window.open(href, "_blank");

However, it comes with the large caveat that the new tab shares a runtime/process with the original, which for a heavy web-app leads to resource problems.然而,它带有一个很大的警告,即新选项卡与原始选项卡共享运行时/进程,这对于繁重的网络应用程序会导致资源问题。

I tried creating an invisible anchor tag and clicking it via JS as a workaround:我尝试创建一个不可见的锚标记并通过 JS 单击它作为解决方法:

const anchor = document.createElement("a");
anchor.setAttribute("href", href);
anchor.setAttribute("target", "_blank");

anchor.click();

but Chrome blocks this as a "popup".但 Chrome 将此作为“弹出窗口”阻止。

There must be some way to do this, surely?肯定有某种方法可以做到这一点,对吧?

I believe, the "noopener" feature runs the window in a seperate process https://developer.mozilla.org/en-US/docs/Web/API/Window/open#noopener我相信,“noopener”功能在一个单独的进程中运行 window https://developer.mozilla.org/en-US/docs/Web/API/Window/open#noopener

let d = window.open('https://www.google.com/', 'name', 'noopener=yes');

with noopener, d would now be null使用 noopener,d 现在是 null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM