windows 如何将已经存在的打开窗口从另一个窗口代码带到其他窗口的前面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2530572/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:11:29  来源:igfitidea点击:

How do I bring an already existing open window to the front on top of other windows from another windows code?

javascripthtmlwindowsjavascript-events

提问by user268249

The question was fairly descriptive but I'll describe it further.

这个问题相当具有描述性,但我会进一步描述它。

Basically, I have window1. Clicking a button link opens window2. Clicking a button in window2opens window3, clicking a button in window3should bring window2back to the front of the screen on top of window2.

基本上,我有window1。单击按钮链接将打开window2。点击一个按钮window2打开window3,点击一个按钮window3应该带window2回到屏幕的前面window2

I'm not sure how this is exactly done, however I have used and played around with focus(), opener and other various methods and I cannot seem to get it to work properly.

我不确定这究竟是如何完成的,但是我已经使用并尝试了 focus()、opener 和其他各种方法,但我似乎无法让它正常工作。

回答by bobince

Update: This hasn't worked since Chrome (21+). The workaround is to close/reopen.

更新:自 Chrome(21 岁以上)起此方法无效。解决方法是关闭/重新打开。

opener.focus()

does work. If it doesn't for you, we'll need a test case.

确实有效。如果它不适合您,我们将需要一个测试用例。

Some things that might cause problems: calling it in an event handler that fires beforethe button's window gets focus due to the click (but I don't think that'd usually be the case); running it on a browser that stuffs pop-ups into browser tabs instead.

一些可能会导致问题的事情:在按钮窗口因点击而获得焦点之前触发的事件处理程序中调用它(但我认为通常情况并非如此);在浏览器上运行它,而不是将弹出窗口填充到浏览器选项卡中。

(I agree with Max's comment. Pop-ups with cross-window scripting are generally best avoided.)

(我同意 Max 的评论。通常最好避免使用跨窗口脚本的弹出窗口。)

回答by brahn

Update: This hasn't worked since Chrome (21+). The workaround is to close/reopen.

更新:自 Chrome(21 岁以上)起此方法无效。解决方法是关闭/重新打开。

The following code works for me on Firefox (Mac & Windows), Safari (Mac & Windows), and IE8 (Windows, of course). I haven't tested IE6 or IE7.

以下代码适用于 Firefox(Mac 和 Windows)、Safari(Mac 和 Windows)和 IE8(当然是 Windows)。我还没有测试过 IE6 或 IE7。

However, it does notwork on Chrome for either Mac or Windows. Specifically, clicking the button once creates the pop-up and brings it to the front. However, returning to the original window and clicking the button again does not refocus the popup.

但是,它并没有在Chrome浏览器无论是Mac或Windows工作。具体来说,单击按钮一次会创建弹出窗口并将其置于最前面。但是,返回到原始窗口并再次单击该按钮不会重新聚焦弹出窗口。

<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>