35

Now I have a link

<a href="blabla" target="_blank">link</a>

However, this always open up a new tab. I want the following effect

  1. If the user already has a tab with the same URL, reuse that tab, and refresh if possible
  2. Otherwise, open a new tab

How can I achieve this using JavaScript?

It's OK if there're only some browser-specific methods, so users of browsers without corresponding support will "fallback" to the always-new-tab way.

Xiao Jia
  • 4,169
  • 2
  • 29
  • 47
  • I don't have enough reputation yet for a comment, so please don't choose this as an answer, but I believe you will find a near-duplicate of this question here: http://stackoverflow.com/questions/2641812/how-to-detect-that-the-same-page-is-already-opened-in-another-window-in-ie Good luck! – Chris Like Dec 08 '12 at 17:06

2 Answers2

35

You can set specific window's name, in order to open reuse the tab. The problem is, as far as the href will be the same, it won't be reloaded. So you can't obtain the refresh part easily.

So, for instance, you can have:

<a href="blabla" target="blabla">link</a>
<a href="foo" target="bar">link</a>

In JS, you can actually obtain the same, using window.open. You could also use the url as target, so that you don't need to specify manually:

<a href="blabla" onclick="window.open(this.href, this.href); return false">link</a>
<a href="foo" onclick="window.open(this.href, this.href); return false">link</a>

You could also generalize, and add a click listener to the document, in order to open some links in this way. Something like:

<div id="container">
    <a href="blabla">link</a>
    <a href="foo">link</a>
</div>

<script>
    document.getElementById("container").onclick = function(evt){
        if (evt.target.tagName === "A")
            window.open(evt.target.href, evt.target.href);

        return false;
    }
</script>

If the page are on the same domain, at this point you could probably trying to do an empiric refresh of the page as well.

ZER0
  • 24,846
  • 5
  • 51
  • 54
  • 3
    Somehow it is not working for me. If I understood the question and answer, there suppose to be, first link click will open the new tab in browser and after any link clicked, same browser tab should be reused... I tried this solution but never worked. It opens new tab every time. Except If trigger same link, it it not opening another tab. – Liladhar Jun 09 '15 at 12:04
  • Note that after second Ctrl+Click browser (at least Chrome) will switch to this tab. That's not really what I would expect. – coolguy Apr 10 '17 at 04:54
  • 2
    Can someone who understands this answer edit this answer into better english. – cda01 Jun 29 '20 at 01:59
  • In my testing, this only works for same domain; all others open in new browser window. – llessurt Mar 07 '23 at 21:09
0

setting up target="child" would refresh the current tab if already opened

<a href="https://abcd.com" target="child">ABcd</a>
<a href="https://defg.com" target="child">Defg</a>
Aman Sadhwani
  • 2,902
  • 3
  • 16
  • 24