Open link in new window or focus to it if already open

前端 未结 6 2191
轮回少年
轮回少年 2020-12-03 02:37

I have a link which should open in a new tab, but if the tab is already open, just switch to it. I\'ve tried with javascript, wnd = window.open() and than wnd.focus(), that

6条回答
  •  情书的邮戳
    2020-12-03 03:21

    In Firefox 66 and Chrome 74 this works for me:

     wnd = window.wnd && window.wnd.close() || window.open(url, "wnd");
    

    Not tested in other browsers. Thanks to roberto (in the comment of the MarkZ answer) to point me on this solution. (I could not add a comment in that answer due my lack of reputation, sorry).

    window.focus() solution was not fit my needs as this other one.

提交回复
热议问题