I would like to have a button on a web page with the following behavior:
我想在网页上有一个按钮,其行为如下:
- On the first click, open a pop-up.
- 在第一次点击,打开一个弹出窗口。
- On later clicks, if the pop-up is still open, just bring it to the front. If not, re-open.
- 在以后的单击中,如果弹出窗口仍然打开,只需将它带到前面。如果没有,重新开放。
The below code works in Firefox (Mac & Windows), Safari (Mac & Windows), and IE8. (I have not yet tested IE6 or IE7.) However, in Google Chrome (both Mac & Windows) later clicks fail to bring the existing pop-up to the front as desired.
下面的代码适用于Firefox (Mac & Windows)、Safari (Mac & Windows)和IE8。(我还没有测试IE6或IE7。)然而,在谷歌浏览器(Mac和Windows都有)中,之后的点击并不能按要求将现有的弹出窗口显示到前面。
How can I make this work in Chrome?
我如何在Chrome中工作?
<head>
<script type="text/javascript">
var popupWindow = null;
var doPopup = function () {
if (popupWindow && !popupWindow.closed) {
popupWindow.focus();
} else {
popupWindow = window.open("http://google.com", "_blank",
"width=200,height=200");
}
};
</script>
</head>
<body>
<button onclick="doPopup(); return false">
create a pop-up
</button>
</body>
Background: I am re-asking this question specifically for Google Chrome, as I think I my code solves the problem at least for other modern browsers and IE8. If there is a preferred etiquette for doing so, please let me know.
背景:我再次问这个问题是关于谷歌Chrome的,因为我认为我的代码至少可以解决其他现代浏览器和IE8的问题。如果有更好的礼仪,请告诉我。
4 个解决方案
#1
7
You can't. Window.focus is disabled in Chrome for security reasons, and that is unlikely to change.
你不能。窗口。由于安全原因,Chrome禁用了focus,这种情况不太可能改变。
You have to close and repopen the respective window.
您必须关闭并重新弹出相应的窗口。
#2
0
Why not just do a popopWindow.focus() after the window.open call? It won't hurt to do it there too, and it should ensure that your new window is shown on top of the current one.
为什么不直接在窗口后执行popopWindow.focus()呢?打开电话吗?这样做也不会有什么坏处,它应该确保您的新窗口显示在当前窗口的顶部。
#3
0
You need to set original window to blur and the new window to focus.
你需要设置原始的窗口来模糊和新的窗口的焦点。
var popup = { //popup = object literal representing your popup
execute = (function () {
popup.win = window.open();
popup.win.focus();
}());
};
window.blur();
#4
0
The following code works for me when called in a onclick handler:
当调用onclick处理程序时,以下代码对我有效:
var popup = window.open('', 'popup-name', 'resizable,width=480=height=575');
if(popup.location == 'about:blank') {
popup.location = 'http://google.com';
return;
}
This code works by trying to access the popup's location, if its about:blank its a new popup and the url is set. Otherwise if the window with the same name 'popup-name' is already open the popup comes into focus. This code does need to be called from a onclick handler otherwise it will not work.
此代码通过尝试访问弹出窗口的位置来工作,如果它是about:blank它是一个新的弹出窗口,url被设置。这段代码需要从onclick处理程序中调用,否则将无法工作。
#1
7
You can't. Window.focus is disabled in Chrome for security reasons, and that is unlikely to change.
你不能。窗口。由于安全原因,Chrome禁用了focus,这种情况不太可能改变。
You have to close and repopen the respective window.
您必须关闭并重新弹出相应的窗口。
#2
0
Why not just do a popopWindow.focus() after the window.open call? It won't hurt to do it there too, and it should ensure that your new window is shown on top of the current one.
为什么不直接在窗口后执行popopWindow.focus()呢?打开电话吗?这样做也不会有什么坏处,它应该确保您的新窗口显示在当前窗口的顶部。
#3
0
You need to set original window to blur and the new window to focus.
你需要设置原始的窗口来模糊和新的窗口的焦点。
var popup = { //popup = object literal representing your popup
execute = (function () {
popup.win = window.open();
popup.win.focus();
}());
};
window.blur();
#4
0
The following code works for me when called in a onclick handler:
当调用onclick处理程序时,以下代码对我有效:
var popup = window.open('', 'popup-name', 'resizable,width=480=height=575');
if(popup.location == 'about:blank') {
popup.location = 'http://google.com';
return;
}
This code works by trying to access the popup's location, if its about:blank its a new popup and the url is set. Otherwise if the window with the same name 'popup-name' is already open the popup comes into focus. This code does need to be called from a onclick handler otherwise it will not work.
此代码通过尝试访问弹出窗口的位置来工作,如果它是about:blank它是一个新的弹出窗口,url被设置。这段代码需要从onclick处理程序中调用,否则将无法工作。