在Google Chrome浏览器中,如何使用父窗口中的JavaScript将现有弹出窗口置于前面? [英] In Google Chrome, how do I bring an existing popup window to the front using javascript from the parent window?
问题描述
我想在网页上有一个按钮,其行为如下:
- 在第一次点击时, 。
- 在稍后的点击中,如果弹出窗口仍处于打开状态,请将其放在最前面。下面的代码适用于Firefox(Mac和Windows),Safari(Mac和Windows),以及Mac OS X和Mac OS X。 ,和IE8。 (我还没有测试IE6或IE7)。但是,在谷歌浏览器(包括Mac和Windows)中,稍后的点击不能将现有弹出窗口按需要放在前面。
如何在Chrome中完成此项工作?
<头>
< 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>
创建弹出式窗口
< / button>
< / body>
背景:我重复询问专门针对谷歌浏览器而开发的,因为我使用了JavaScript认为我的代码至少可以解决其他现代浏览器和IE8的问题。如果有这样做的首选礼仪,请告诉我。 你不能。由于安全原因,Chrome浏览器中禁用了Window.focus,并且这种情况不太可能发生变化。
您必须关闭并重新打开相应的窗口。
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.
How can I make this work in 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.
解决方案You can't. Window.focus is disabled in Chrome for security reasons, and that is unlikely to change.
You have to close and repopen the respective window.
这篇关于在Google Chrome浏览器中,如何使用父窗口中的JavaScript将现有弹出窗口置于前面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!