如果已经在window.open中打开,则将窗口置于前面?

时间:2022-10-22 14:58:15

If you open a window like:

如果您打开一个窗口,如:

window.open ("url","winName","location=0,width=300,height=214");

If winName is already open it just changes the URL in the window. This is ok but if that window is behind the current window most users won't realize this and think that it is just not opening.

如果winName已经打开,它只会更改窗口中的URL。这没关系,但如果那个窗口在当前窗口后面,大多数用户都不会意识到这一点,并认为它只是没有打开。

Is there any way that if the window is already open, it brings it to the front?

有没有办法,如果窗口已经打开,它会把它带到前面?

7 个解决方案

#1


45  

Update: This hasn't worked since Chrome (21+). The workaround is to close/reopen.

更新:自Chrome(21+)以来,此功能无效。解决方法是关闭/重新打开。

The window.open() method returns an object that represents the new window. You just need to window.focus() it:

window.open()方法返回表示新窗口的对象。你只需要window.focus()它:

var w = window.open ("url","winName","location=0,width=300,height=214");
w.focus();

Or simply:

window.open("url","winName","location=0,width=300,height=214").focus();

#2


9  

The various answers suggesting using any form of .focus() are likely not going to work in all browsers.

建议使用任何形式的.focus()的各种答案可能不适用于所有浏览器。

This used to work back in the day but not any more, mainly due to browsers working to actively stop shady ad networks from pushing their popup ads to the foreground.

这曾经在当天恢复工作,但现在已经没有了,主要是因为浏览器正在积极阻止阴影广告网络将其弹出广告推向前台。

In Mozilla Firefox in particular (depending on your version) there is a configuration setting that is turned on by default that stops other windows (e.g. popups) from focusing themselves.

特别是在Mozilla Firefox中(取决于您的版本),默认情况下会启用一个配置设置,以阻止其他窗口(例如弹出窗口)自行聚焦。

You can find this setting in the about:config page (tread carefully!)

您可以在about:config页面中找到此设置(谨慎行事!)

dom.disable_window_flip: true

If I recall correctly this setting used to be called something like ~"allow_raise_or_lower_windows*

如果我没记错的话,这个设置曾被称为〜“allow_raise_or_lower_windows *

Other browsers may implement something similar, but quite simply if 1 of the major browsers blocks the use of .focus() by default then there's not much use in attempting to call it.

其他浏览器可能会实现类似的东西,但非常简单,如果主要浏览器中的一个默认阻止使用.focus(),那么在尝试调用它时没有多大用处。

As a result, the only solution I've seen that works is to see if the window exists, and is not already closed... and if so close it, then load the window you want.

因此,我看到的唯一解决方案是查看窗口是否存在,并且尚未关闭...如果关闭它,则加载所需的窗口。

function closePopupIfOpen(popupName){
  if(typeof(window[popupName]) != 'undefined' && !window[popupName].closed){
    window[popupName].close();
  }
}

when opening your popup if there's a chance it is already open (and burried behind other windows) then you can call this function before you attempt to open your popup.

当打开你的弹出窗口时,如果它有可能已经打开(并埋在其他窗口后面),那么你可以在尝试打开弹出窗口之前调用此函数。

closePopupIfOpen('fooWin');
var fooWin = window.open('someURL', 'foo', '...features...');

The drawback of course is that if there was anything "important" (e.g. a form partially filled in) in that window it will be lost.

当然,缺点是如果在该窗口中有任何“重要”(例如,部分填写的表格),它将丢失。

#3


7  

Update: This hasn't worked since Chrome (21+). The workaround is to close/reopen.

更新:自Chrome(21+)以来,此功能无效。解决方法是关闭/重新打开。

Be careful, because when you open a new window, the opener window might still have some code to be executed, maybe some of this code gives it the focus. You would see how your new window opens in the front and suddenly goes to the back, so, it is a great idea in these cases, to set a timeout in order to give the focus to the new window a bit later on, when all the javascript in the opener window is executed, you can do it this way:

要小心,因为当你打开一个新窗口时,开启窗口可能仍然有一些代码要执行,也许这些代码中的一些代码使它成为焦点。您会看到新窗口如何在前面打开然后突然转到后面,因此,在这些情况下,设置超时以便稍后将焦点放在新窗口上是一个好主意。在opener窗口中执行javascript,你可以这样做:

    setTimeout(function(){window.focus();},1000);

Being 1000 the amount of miliseconds to wait, and window the name of the opened window. You could also use this code in the opened window in the body onload for example.

为1000等待的毫秒数,并窗口打开窗口的名称。例如,您也可以在body onload的打开窗口中使用此代码。

#4


3  

I fixed this by adding

我通过添加修复此问题

onclick="myWin = window.open('','winName','location=0,width=300,height=214'); myWin.focus()"

to the html element(button) and then change the URL via JS.

到html元素(按钮),然后通过JS更改URL。

#5


2  

window.focus() applied to the window in question should do the trick.

应用于有问题的窗口的window.focus()应该可以解决问题。

#6


0  

You can use jQuery :

你可以使用jQuery:

myPopup = window.open(url, "Title","menubar=no, status=no, scrollbars=no, menubar=no, width=800, height=800");
$(myPopup).focus();

#7


0  

Closing the window first, does the trick for me:

首先关闭窗口,对我有用:

window.open('', 'mypopup').close();
setTimeout(function() {
    window.open('', 'mypopup').focus();
}, 500);

#1


45  

Update: This hasn't worked since Chrome (21+). The workaround is to close/reopen.

更新:自Chrome(21+)以来,此功能无效。解决方法是关闭/重新打开。

The window.open() method returns an object that represents the new window. You just need to window.focus() it:

window.open()方法返回表示新窗口的对象。你只需要window.focus()它:

var w = window.open ("url","winName","location=0,width=300,height=214");
w.focus();

Or simply:

window.open("url","winName","location=0,width=300,height=214").focus();

#2


9  

The various answers suggesting using any form of .focus() are likely not going to work in all browsers.

建议使用任何形式的.focus()的各种答案可能不适用于所有浏览器。

This used to work back in the day but not any more, mainly due to browsers working to actively stop shady ad networks from pushing their popup ads to the foreground.

这曾经在当天恢复工作,但现在已经没有了,主要是因为浏览器正在积极阻止阴影广告网络将其弹出广告推向前台。

In Mozilla Firefox in particular (depending on your version) there is a configuration setting that is turned on by default that stops other windows (e.g. popups) from focusing themselves.

特别是在Mozilla Firefox中(取决于您的版本),默认情况下会启用一个配置设置,以阻止其他窗口(例如弹出窗口)自行聚焦。

You can find this setting in the about:config page (tread carefully!)

您可以在about:config页面中找到此设置(谨慎行事!)

dom.disable_window_flip: true

If I recall correctly this setting used to be called something like ~"allow_raise_or_lower_windows*

如果我没记错的话,这个设置曾被称为〜“allow_raise_or_lower_windows *

Other browsers may implement something similar, but quite simply if 1 of the major browsers blocks the use of .focus() by default then there's not much use in attempting to call it.

其他浏览器可能会实现类似的东西,但非常简单,如果主要浏览器中的一个默认阻止使用.focus(),那么在尝试调用它时没有多大用处。

As a result, the only solution I've seen that works is to see if the window exists, and is not already closed... and if so close it, then load the window you want.

因此,我看到的唯一解决方案是查看窗口是否存在,并且尚未关闭...如果关闭它,则加载所需的窗口。

function closePopupIfOpen(popupName){
  if(typeof(window[popupName]) != 'undefined' && !window[popupName].closed){
    window[popupName].close();
  }
}

when opening your popup if there's a chance it is already open (and burried behind other windows) then you can call this function before you attempt to open your popup.

当打开你的弹出窗口时,如果它有可能已经打开(并埋在其他窗口后面),那么你可以在尝试打开弹出窗口之前调用此函数。

closePopupIfOpen('fooWin');
var fooWin = window.open('someURL', 'foo', '...features...');

The drawback of course is that if there was anything "important" (e.g. a form partially filled in) in that window it will be lost.

当然,缺点是如果在该窗口中有任何“重要”(例如,部分填写的表格),它将丢失。

#3


7  

Update: This hasn't worked since Chrome (21+). The workaround is to close/reopen.

更新:自Chrome(21+)以来,此功能无效。解决方法是关闭/重新打开。

Be careful, because when you open a new window, the opener window might still have some code to be executed, maybe some of this code gives it the focus. You would see how your new window opens in the front and suddenly goes to the back, so, it is a great idea in these cases, to set a timeout in order to give the focus to the new window a bit later on, when all the javascript in the opener window is executed, you can do it this way:

要小心,因为当你打开一个新窗口时,开启窗口可能仍然有一些代码要执行,也许这些代码中的一些代码使它成为焦点。您会看到新窗口如何在前面打开然后突然转到后面,因此,在这些情况下,设置超时以便稍后将焦点放在新窗口上是一个好主意。在opener窗口中执行javascript,你可以这样做:

    setTimeout(function(){window.focus();},1000);

Being 1000 the amount of miliseconds to wait, and window the name of the opened window. You could also use this code in the opened window in the body onload for example.

为1000等待的毫秒数,并窗口打开窗口的名称。例如,您也可以在body onload的打开窗口中使用此代码。

#4


3  

I fixed this by adding

我通过添加修复此问题

onclick="myWin = window.open('','winName','location=0,width=300,height=214'); myWin.focus()"

to the html element(button) and then change the URL via JS.

到html元素(按钮),然后通过JS更改URL。

#5


2  

window.focus() applied to the window in question should do the trick.

应用于有问题的窗口的window.focus()应该可以解决问题。

#6


0  

You can use jQuery :

你可以使用jQuery:

myPopup = window.open(url, "Title","menubar=no, status=no, scrollbars=no, menubar=no, width=800, height=800");
$(myPopup).focus();

#7


0  

Closing the window first, does the trick for me:

首先关闭窗口,对我有用:

window.open('', 'mypopup').close();
setTimeout(function() {
    window.open('', 'mypopup').focus();
}, 500);