简体   繁体   English

Window.open - 覆盖浏览器窗口选项卡

[英]Window.open - Override browser window tab

Let's say I have two web apps.假设我有两个网络应用程序。

In my first web app, there is a button that uses window.open and when a user clicks that button it opens a new tab with a new url link.在我的第一个 Web 应用程序中,有一个使用 window.open 的按钮,当用户单击该按钮时,它会打开一个带有新 url 链接的新选项卡。 When it opens the new tab, it puts a target name to that tab.当它打开新选项卡时,它会在该选项卡上放置一个目标名称。

For example, window.open(url, "foo");例如 window.open(url, "foo"); The new tab opens the url with the target name "foo".新选项卡打开目标名称为“foo”的 url。

Now, I have a second web app and this one also has a button that uses window.open.现在,我有第二个 Web 应用程序,这个应用程序还有一个使用 window.open 的按钮。 If a user clicks this button, I want to override any tab with name "foo" whether the tab was opened from the first web app or second web app.如果用户单击此按钮,我想覆盖名称为“foo”的任何选项卡,无论该选项卡是从第一个 Web 应用程序还是从第二个 Web 应用程序打开。

In the second web app, it uses window.open(url, "foo") as well.在第二个 Web 应用程序中,它也使用 window.open(url, "foo") 。 Exactly the same code as the first web app.与第一个 Web 应用程序完全相同的代码。

The problem is that the tab is only replaced when it is created from the same web app.问题是该选项卡仅在从同一 Web 应用程序创建时才被替换。 If a user clicks the button and create a new tab from the first web app and the user clicks the button from second web app, rather it overrides the tab from the first web app but it creates another new tab although the target name is same.如果用户单击按钮并从第一个 Web 应用程序创建一个新选项卡,而用户单击第二个 Web 应用程序中的按钮,则它会覆盖第一个 Web 应用程序中的选项卡,但会创建另一个新选项卡,尽管目标名称相同。

Is there any way that I can override any tab that has target name?有什么方法可以覆盖任何具有目标名称的选项卡?

No, and this is a security feature.不,这是一项安全功能。

I can't find a reference link, but an additional requirement for window reuse is that the window.opener attribute must be the same.我找不到参考链接,但对窗口重用的附加要求是window.opener属性必须相同。

So, for example, if you had two links like these:因此,例如,如果您有两个这样的链接:

<a href="https://en.wikipedia.org/wiki/Foo" target="foo">Foo on Wikipedia</a>
<a href="https://en.wikipedia.org/wiki/Bar" target="foo">Bar on Wikipedia</a>

then clicking on each one would re-use the same window, because the target is the same and because the window containing the links matches the value of window.opener on the target window.然后单击每个将重新使用相同的窗口,因为目标是相同的,并且因为包含链接的窗口与目标窗口上的window.opener的值匹配。

If you added rel="noopener" to the links like this:如果您将rel="noopener"添加到这样的链接中:

<a href="https://en.wikipedia.org/wiki/Foo" target="foo" rel="noopener">Foo on Wikipedia</a>
<a href="https://en.wikipedia.org/wiki/Bar" target="foo" rel="noopener">Bar on Wikipedia</a>

then the window would not be re-used (even though the target matches) because the value of window.opener would not match the origin window.那么该窗口将不会被重新使用(即使目标匹配),因为window.opener的值将与原始窗口不匹配。

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

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