在新选项卡中打开网址或尽可能重复使用现有网址 [英] open url in new tab or reuse existing one whenever possible

查看:92
本文介绍了在新选项卡中打开网址或尽可能重复使用现有网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有一个链接

 < a href =blablatarget =_ blank>链接<一个> 

然而,这总是会打开一个新标签页。我希望获得以下效果:如果用户已经有一个具有相同URL的选项卡,请重复使用该选项卡,并在可能的情况下进行刷新 >

  • 否则,打开一个新标签

  • 如何使用JavaScript实现此功能?



    如果只有一些浏览器特定的方法,那么没问题,所以没有相应支持的浏览器用户将会后退到always-new-tab方式。

    解决方案

    您可以设置特定窗口的名称,以便打开重用选项卡。问题是,只要href将相同,它将不会被重新加载。所以你不能轻易获得刷新部分。

    所以,例如,你可以有:

     < a href =blablatarget =blabla>链接< / a> 
    < a href =footarget =bar>连结< / a>

    在JS中,您可以使用 window.open 。您也可以将网址作为 target ,这样就不需要手动指定:

     < a href =blablaonclick =window.open(this.href,this.href); return false> link< / a> 
    < a href =fooonclick =window.open(this.href,this.href); return false>链接< / a>

    您也可以概括并添加一个点击侦听器到文档中,以打开一些链接这条路。例如:

     < div id =container> 
    < a href =blabla>连结< / a>
    < a href =foo>连结< / a>
    < / div>

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

    返回false;
    }
    < / script>

    如果页面位于同一个域中,此时您可能尝试进行经验刷新的页面。


    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.

    解决方案

    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.

    这篇关于在新选项卡中打开网址或尽可能重复使用现有网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆