Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
587 views
in Technique[技术] by (71.8m points)

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

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 window2 opens window3, clicking a button in window3 should bring window2 back to the front of the screen on top of 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

However, it does not work 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.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...