问题描述
我想在网页上有一个具有以下行为的按钮:
I would like to have a button on a web page with the following behavior:
- 第一次点击时,打开一个弹出窗口.
- 在以后的点击中,如果弹出窗口仍然打开,只需将其带到前面即可.如果没有,请重新打开.
以下代码适用于 Firefox(Mac 和 Windows)、Safari(Mac 和 Windows)和 IE8.(我还没有测试过 IE6 或 IE7.)但是,在 Google Chrome(Mac 和 Windows)中,稍后单击无法将现有的弹出窗口按需要置于最前面.
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.
如何在 Chrome 中进行这项工作?
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>
背景:我正在重新询问 this question 专门针对 Google Chrome,因为我认为我的代码至少解决了其他现代浏览器和 IE8 的问题.如果有这样做的首选礼仪,请告诉我.
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 中禁用了 Window.focus,这不太可能改变.
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 将现有的弹出窗口置于最前面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!