如果已经在 window.open 中打开,Javascript 将窗口置于前面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3311293/
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
Javascript Bring window to front if already open in window.open?
提问by JD Isaacks
If you open a window like:
如果您打开一个窗口,例如:
window.open ("url","winName","location=0,width=300,height=214");
If winNameis 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?
如果窗户已经打开,有什么办法可以把它带到前面吗?
回答by álvaro González
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();
回答by scunliffe
The various answers suggesting using any form of .focus()are likely notgoing to work in all browsers.
建议使用任何形式的各种答案.focus()可能不适用于所有浏览器。
This used to workback 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 defaultthat stops other windows (e.g. popups) from focusing themselves.
特别是在 Mozilla Firefox 中(取决于您的版本),默认情况下会启用一个配置设置,以阻止其他窗口(例如弹出窗口)聚焦自身。
You can find this setting in the about:configpage (tread carefully!)
您可以在about:config页面中找到此设置(请仔细阅读!)
dom.disable_window_flip: true
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 mayimplement 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.
当然,缺点是如果该窗口中有任何“重要”的东西(例如部分填写的表格),它将丢失。
回答by nahiko
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,而 window 是打开的窗口的名称。例如,您还可以在主体 onload 的打开窗口中使用此代码。
回答by Robusto
window.focus()applied to the window in question should do the trick.
window.focus()应用于有问题的窗口应该可以解决问题。
回答by Raffy Cortez
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。
回答by appcropolis
Closing the window first, does the trick for me:
首先关闭窗口,对我有用:
window.open('', 'mypopup').close();
setTimeout(function() {
window.open('', 'mypopup').focus();
}, 500);
回答by Shessuky
I had the same problem, have spent a lot of time to find a solution, finally it works by this:
我遇到了同样的问题,花了很多时间找到解决方案,最后它是这样工作的:
var singleWindow;
function openNewWindow(){
if(singleWindow){
singleWindow.close();
}
singleWindow = window.open ("url","winName","location=0,width=300,height=214");
}
Hope this help you !
希望这对你有帮助!
回答by LMARCHAL
You can use jQuery :
您可以使用 jQuery :
myPopup = window.open(url, "Title","menubar=no, status=no, scrollbars=no, menubar=no, width=800, height=800");
$(myPopup).focus();

