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

前端 未结 6 2190
轮回少年
轮回少年 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:19

    You shouldn't need any logic for something like this. By default, specifying the second parameter for window.open() gives the window a "name", that the browser remembers. If you try to call window.open() with the same name (after it's already been opened), it doesn't open a new window...but you might still need to call .focus() on it. Try this:

    var a = window.open(url, "name");
    a.focus();
    

    Those should be the only lines of code in your function, and you don't need the loadingTableWnd variable...

提交回复
热议问题