window.focus() not working in Google Chrome

后端 未结 16 1693
余生分开走
余生分开走 2020-11-28 05:07

Just wondering if Google Chrome is going to support window.focus() at some point. When I mean support, I mean have it work. The call to it doesn\'t fail, it jus

相关标签:
16条回答
  • 2020-11-28 05:32

    Here's a workaround I was able to use. It may not work for everybody, but it does what I need it to, and it does handle scenarios where your popup has been updated via Ajax after the initial load (ie - it doesn't go back to the server to reload the page).

    function refocusWindow() {
      var newName = window.name + '-2'; // you'll want to customize this for your needs
      var options = ''; // again, customize for your situation
      var w = window.open('', newName, options);
      w.document.write(document.getElementById('everything').innerHTML);
      window.close();
    }    
    

    I'm using the new window trick to make it look like I'm just refocusing the page, but I'm actually creating a new window with the exact contents of the old window and then closing the old window.

    You'll just need to make sure that you are able to grab everything you need for the new window. I put everything I need in an #everything div; you may need to change this for your needs.

    Hope this at least helps some of you.

    Note: inline Javascript seems to execute with this approach, linked Javascript may not. Proceed with caution if this will be a problem for you.

    0 讨论(0)
  • 2020-11-28 05:33

    you can focus another window by calling open with javascript:; as url parameter:

    window.open('http://....', 'myWindow');
    
    // focus that window later on...
    window.open('javascript:;', 'myWindow');
    
    0 讨论(0)
  • 2020-11-28 05:34

    window.focus() for child windows works fine for me on Chrome v52 on Windows, but ONLY inside a user-initiated event, e.g. a click callback. (This is the same restriction that the pop-up blocked applies to window.open() calls.)

    0 讨论(0)
  • 2020-11-28 05:36

    I've been struggling with this issue. I wanted a reference to another window, so I was issuing a:

    otherWinRef = window.open("","OtherWindow");
    

    However when I issue this command, the browser will switch focus to the OtherWindow. I thought this could be addressed by doing this:

    otherWinRef = window.open("","OtherWindow");
    window.focus();
    

    but the window.focus() has no effect. I tried:

    otherWinRef = window.open("","OtherWindow");
    setTimeout(window.focus,0);
    

    But the window.focus() call still had no effect.

    I resolve the issue by adding the following code to the OtherWindow's source.

    function Bounce(w) {
    
            window.blur();
            w.focus();
    }
    

    Then I changed the code in the main window to:

    otherWinRef = window.open("","OtherWindow");
    otherWinRef.Bounce(window);
    
    0 讨论(0)
  • 2020-11-28 05:36

    I just found out a quite simple solution.

    If you reopen a window that is in a background position, targeting the same window ("_self"), Chrome automatically focus that window.

    To regain focus of a window you should use the following code:

    path = windowHandle.document.URL;
    windowHandle.open(path,"_self");
    
    0 讨论(0)
  • 2020-11-28 05:38

    I fought this in Chrome. I wanted a popup window to display when the user clicked a link on the parent screen. Works the first time the popup window is displayed, but once the popup loses focus, javascript can't bring it back to the front again; you must manually click on the window. Here is what worked for me. This script is the parent window (the one with the links). As you can see, I put it at the end of the HEAD section::

    <script type="text/javascript">
      var infoWindow;
    
      function openLink(url)   {
        infoWindow = window.open("", "infoWindow", "width=880, height=500, top=20, left=20, scrollbars=yes, toolbar=yes, resizable=yes");
        infoWindow.location.href=url;
        infoWindow.focus();
      }
    
      function closeWindow()   {
        if (infoWindow)   {
          infoWindow.close();
        }
      }
    </script>
    </head>
    <body bgcolor="black" onFocus="closeWindow()" onLoad="closeWindow()">
    

    All links are <A href="Javascript: openLink('url')">. If the user does not close the popup window manually, it is closed when the parent window regains focus. So the popup is destroyed and re-created every time. Seems kinda convoluted, but it works for me.

    0 讨论(0)
提交回复
热议问题