4

When my website is called from an external hyperlink it shall be re-opened within the same browser tab, if it is already open. This can be achieved by specifying an appropriate target attribute within the hyperlink's HTML: <a href='http://my-website.org' target='my_target' /> However, this does not work, if the first tab is opened without a target being specified, e. g. by directly typing in the website's url. After this the hyperlink using my_target will pull up a second browser tab.

So, I want to set a default target name of the browser tab, if none exists, to ensure that this tab is used, if this default target is used in hyperlinks.

How can this be done in JavaScript?

Sir Hackalot
  • 521
  • 6
  • 16

2 Answers2

5

Think all you have to do is

window.name = 'my_target'

If this is the first tab it will give mark this as 'my_target'. When any link with

target='my_target'

is now clicked it will opened in this tab/window.

Verim
  • 1,065
  • 9
  • 17
  • Thanks, this works like a charm, if I pull up the url via javascript (window.open). However, if I try the same via a static hyperlink a separate tab is opened (but correctly reused, if I click the static hyperlink again) - as if I was using a different target here. But I'm not. I triple-checked twice: those targets I use are the same! Any idea? – Sir Hackalot Feb 11 '15 at 14:20
  • Verim, thanks for testing that! Your code makes perfect sense, however, still not working on my side (testing with FF). My guesswork is that some browser settings of the weird kind force FF to open those static links in new tabs regardless of the specified target. Will check – Sir Hackalot Feb 12 '15 at 22:13
  • Works FF 47.0.1, but doesn't work in Chrome 54.0.2840.71 – ENargit Nov 08 '16 at 11:18
  • Doesn't work on Chrome. I named my tab, then emailed myself a link with the same target name, click link and nothing happens. – davidtgq Jun 27 '19 at 21:30
  • Is it possible a service could disable the same tab linking for older browsers by changing window.name on page init/load? – MrMesees Dec 09 '21 at 05:11
1

Put an underscore in front of the name: _my_target. That has always worked for me. However, I do see that names other than the standard ones (_blank, _parent, _self, _top) are known as a framename, and frames are now deprecated. Still, any custom name with a leading underscore does seem to work. The advantage is that links to the same named target will give focus to the associated browser tab. The bad news is that it will probably stop working some day as browsers update.

Ray N. Franklin
  • 595
  • 1
  • 5
  • 13