简体   繁体   English

在Google Chrome浏览器中,如何使用父窗口中的javascript将现有的弹出窗口置于最前面?

[英]In Google Chrome, how do I bring an existing popup window to the front using javascript from the parent window?

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. 以下代码可在Firefox(Mac和Windows),Safari(Mac和Windows)和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. (我尚未测试IE6或IE7。)但是,在Google Chrome浏览器(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. 背景:我再次问这个问题,专门为谷歌Chrome,因为我觉得我在我的代码至少解决问题的其他现代浏览器和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. 出于安全原因,Chrome中的Window.focus已被禁用,并且这种情况不太可能更改。

You have to close and repopen the respective window. 您必须关闭并重新打开相应的窗口。

Why not just do a popopWindow.focus() after the window.open call? 为什么不只在window.open调用之后执行popopWindow.focus()? 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. 在那里也这样做不会有什么坏处,它应该确保新窗口显示在当前窗口的顶部。

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();

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. 这段代码通过尝试访问弹出窗口的位置来工作,如果它的about:blank是一个新的弹出窗口并且设置了URL。 Otherwise if the window with the same name 'popup-name' is already open the popup comes into focus. 否则,如果已经打开了具有相同名称“ popup-name”的窗口,则弹出窗口将成为焦点。 This code does need to be called from a onclick handler otherwise it will not work. 确实需要从onclick处理程序中调用此代码,否则它将无法正常工作。

As of today, just calling focus() on the reference to the popup just works and brings the window to the front. 从今天开始,仅在对弹出窗口的引用上调用focus()即可起作用,并将窗口置于最前面。 I am using Chrome version 62 我正在使用Chrome版本62

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从另一个Windows代码中将已存在的打开窗口置于其他窗口的正面? - How do I bring an already existing open window to the front on top of other windows from another windows code? JavaScript-如何将输入值从弹出窗口传递到Chrome中的父窗口? - JavaScript - How to pass input values from popup to parent window in Chrome? 如何在不“切换”电子应用程序的情况下将窗口置于最前面? - How do I bring a window to the front, without "switching" apps in electron? 如何始终使用JavaScript将窗口置于最前面? - How to always bring a window to the front with Javascript? 3秒钟后如何使用jQuery或Javascript将浏览器置于所有其他窗口或浏览器的前面/焦点? - How can I bring browser to front/ in focus of all other window or browsers after 3 seconds using jQuery or Javascript? 使用Javascript从弹出窗口调用父窗口函数 - calling parent window functions from popup window using Javascript 我正在使用javascript打开新窗口弹出窗口,这将限制用户使用其父窗口 - i am opening new window popup using javascript which will restrict user from using its parent window 如何从iframe窗口打开Java弹出窗口到父窗口 - How to open Javascript popup from iframe window to parent window 如何在javascript中从子弹出窗口显示父窗口中的警报? - How can I show alert in parent window from child popup window in javascript? 在javascript中打开子弹出窗口后,如何从子窗口访问父窗口? - How can I access a parent window from a child window after a child popup is opened in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM