javascript 将弹出窗口放在前面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9008835/
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
Bring popup to front
提问by Ricardo Binns
In my application, I have a popup with information that open when I choose some options.
在我的应用程序中,当我选择某些选项时,会弹出一个包含信息的弹出窗口。
First time it's OK, popup highlights in front of everything.
第一次没问题,弹出窗口突出显示所有内容。
But, when it loses focus, when the user goes to other window, if user clicks again in the same option, I want that the popup shows up again, in front of everything.
但是,当它失去焦点时,当用户转到其他窗口时,如果用户再次单击同一选项,我希望弹出窗口再次显示在所有内容的前面。
I tried something like:
我试过类似的东西:
<body onLoad="this.focus()">
window.focus();
document.focus();
this.focus();
but it does not work for me.
但这对我不起作用。
What am I missing ?
我错过了什么?
回答by ShankarSangoli
You should maintain a reference to the popup window you open and call focus method on it.
您应该维护对您打开的弹出窗口的引用,并在其上调用 focus 方法。
var win = window.open(url, name, args);
win.focus();
While opening a popup if you give a name and next time when you open a popup with the same name it will use the same popup if it is not closed. You just have to focus it.
在打开一个弹出窗口时,如果您给出一个名称,下次当您打开一个具有相同名称的弹出窗口时,如果它没有关闭,它将使用相同的弹出窗口。你只需要集中注意力。
You can use this simple function to handle popups
您可以使用这个简单的函数来处理弹出窗口
function windowOpener(url, name, args) {
if(typeof(popupWin) != "object" || popupWin.closed) {
popupWin = window.open(url, name, args);
}
else{
popupWin.location.href = url;
}
popupWin.focus();
}