Javascript 在新窗口中打开链接,如果已经打开,请关注它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10982593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:52:29  来源:igfitidea点击:

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

javascripthtml

提问by jaraics

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 works in Chrome 19, but not in FF 13 or IE 9. Here's the code I've written :

我有一个应该在新选项卡中打开的链接,但如果该选项卡已经打开,只需切换到它。我已经尝试过使用 javascript wnd = window.open() 而不是 wnd.focus(),它在 Chrome 19 中有效,但在 FF 13 或 IE 9 中无效。这是我编写的代码:

<script type="text/javascript">
var loadingTableWnd;
function openOrSwitchToWindow(url){
if(loadingTableWnd == undefined)
loadingTableWnd = window.open(url,'myFrame');
else
loadingTableWnd.focus();
}
</script> 
<a href='javascript:openOrSwitchToWindow("/");' >Loading Table</a>

Any idea how can I open or switch to from every browser?

知道如何打开或切换到每个浏览器吗?

EDIT: I need to open the link in a new tab, not a stand-alone window.

编辑:我需要在新选项卡而不是独立窗口中打开链接。

回答by jaraics

Different browsers behave differently for window.open() and focus(). For this code window.open('www.sample.com','mywindow').focus()

不同的浏览器对 window.open() 和 focus() 有不同的表现。对于此代码window.open('www.sample.com','mywindow').focus()

  • Chrome 20opens a new tab, and focuses on subsequent open() calls regardless if focus() is called or not.
  • Firefox 13opens a new tab, focuses on first open(), does not focus on subsequent open() calls/disregards focus().
  • IE 8opens a new window, honors focus().
  • Safari 5opens a new window, and focuses on subsequent open() calls regardless if focus() is called or not.
  • Chrome 20 会打开一个新选项卡,并专注于后续的 open() 调用,无论是否调用了 focus()。
  • Firefox 13打开一个新标签页,关注第一个 open(),不关注后续的 open() 调用/忽略 focus()。
  • IE 8打开一个新窗口,显示 focus()。
  • Safari 5打开一个新窗口,并专注于后续的 open() 调用,无论是否调用 focus()。

Fiddle to test with: http://jsfiddle.net/jaraics/pEG3j/

小提琴测试:http: //jsfiddle.net/jaraics/pEG3j/

回答by Ian

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:

对于这样的事情,您不应该需要任何逻辑。默认情况下,指定第二个参数 forwindow.open()为窗口提供一个浏览器记住的“名称”。如果您尝试window.open()使用相同的名称(在它已经打开之后)调用,它不会打开一个新窗口……但您可能仍然需要调用.focus()它。尝试这个:

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 loadingTableWndvariable...

这些应该是您的函数中唯一的代码行,并且您不需要loadingTableWnd变量...

回答by dileepar

If the window is already opened and if you want to focus on that window you can use

如果窗口已经打开,并且您想专注于该窗口,则可以使用

window.open('', 'NameOfTheOpenedWindow').focus();

回答by MarkZ

If you are not interested in retaining the state of a previously opened tab, you can do this:

如果您不想保留以前打开的选项卡的状态,您可以这样做:

var loadingTableWnd;
function openOrSwitchToWindow(url) {
    if (loadingTableWnd != undefined) {
        loadingTableWnd.close();
    }
    loadingTableWnd = window.open(url,'myFrame');
}

回答by Sergey Rybalkin

window.focus()is widely supportedand seems to be working fine in both Internet Explorer and Firefox for me, the problem should be in your code. I've created a simple jsFiddlefor you to test.

window.focus()得到广泛支持,对我来说似乎在 Internet Explorer 和 Firefox 中都可以正常工作,问题应该出在您的代码中。我已经创建了一个简单的jsFiddle供您测试。

回答by masterguru

In Firefox 66 and Chrome 74 this works for me:

在 Firefox 66 和 Chrome 74 中,这对我有用:

 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).

未在其他浏览器中测试。感谢 roberto(在 MarkZ 答案的评论中)向我指出这个解决方案。(由于我缺乏声誉,我无法在该答案中添加评论,抱歉)。

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

window.focus() 解决方案不适合我的需求,因为另一个。