open url in new tab or reuse existing one whenever possible

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="https://stackoverflow.com/questions/13779508/blabla" target="https://stackoverflow.com/questions/13779508/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="https://stackoverflow.com/questions/13779508/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="https://stackoverflow.com/questions/13779508/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.

Leave a Comment