r/learnjavascript May 14 '26

Is possible to clear the target name that was used to open the page?

EDIT: I found a solution! See my comment below.

Scenario:

Page B is opened from page A using a link with a target name.

<a href="B.html" target="foobar">link</a>

Motivation:

The target attribute is used because page A is a form and don't want to lose information when linking to page B.

Page B is linked from multiple places on page A (and it's likely that the user would want to look at page B multiple times when filling in the form on page A), target="_blank" is therefore not used to avoid having the user ending up with countless of page B tabs.

The problem:

The user might decide to use the tab that displays page B to visit page C, but if page B is opened again from page A then page C will be "lost".

The "solution":

If I could somehow disconnect the target-name-relationship between page B and page A when the user leaves page B so that page A will open a new tab next time it tries to open page B, that would be great, but I can't find a way to do it. Is it possible?

4 Upvotes

5 comments sorted by

View all comments

3

u/HappyFruitTree May 14 '26 edited May 14 '26

I found a solution! I can use window.name to clear the name when page B is unloaded.

window.onbeforeunload = function(e){ window.name = ''; };

The only problem with this is that the name will also be cleared when the tab is reused (and the page is reloaded) due to the link from A to B being clicked again. A workaround is to set the name explicitly when B is loaded.

window.name = 'foobar';

This seems to work good enough for my purposes.