简体   繁体   中英

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. When it opens the new tab, it puts a target name to that tab.

For example, window.open(url, "foo"); The new tab opens the url with the target name "foo".

Now, I have a second web app and this one also has a button that uses 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.

In the second web app, it uses window.open(url, "foo") as well. Exactly the same code as the first web app.

The problem is that the tab is only replaced when it is created from the same web app. 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.

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.

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.

If you added rel="noopener" to the links like this:

<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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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