window.focus() not working in Google Chrome

后端 未结 16 1692
余生分开走
余生分开走 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:21

    This works fine for me. Removed launching blank window from catch block, instead launching the url directly, which avoids user's confusion when he says blank window.

    windowHandle = window.open('', 'PrintInvoice', urlOptions);
    try {
        windowHandle.document.location.href = url;
    } catch (exc) {
        windowHandle.close();
        windowHandle = window.open(url, 'PrintInvoice', urlOptions);
    }
    windowHandle.focus();
    
    0 讨论(0)
  • 2020-11-28 05:22

    Maybe not what everyone wants either, but (in chrome) I noticed that an alert raised from the pop uped page keeps hold on the focus of the popup. So I'm doing this now...

    On the pop uped page, have a function loaded in the header:

    <script type="text/javascript">
        function confirmBlur() {
            if (confirm("Do you want to close this window?")) {
                window.close();
            }
        }
    </script>`
    

    And then on the pop uped page:

    <body onblur="ConfirmBlur()">
    

    So this is a variant of closing the popup when the focus is lost. But in my case, the popup is an edit window for data. A window, one doesn't want to close unasked. So now the user gets notified the focus is about to drop and gets a choice if he wants to close the window or not. Again, far from perfect, but it worked for me.

    (be gentle)

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

    The only solution that currently works in Chrome is this code inside new window:

    $(".closeBtn").click( function(e) 
    {
        window.open("",window.opener.name);
        window.close();
    });
    

    Unfortunately the solution only works under two conditions:

    • window.opener has to have it's name set on document load (window.name="WhateverName";)
    • window.open() is called on user click
    0 讨论(0)
  • 2020-11-28 05:26

    Still the same in version 14.0.835.202 m on Windows 7; found another workaround, not more elegant but at least will avoid losing data on the page: show an alert in the window you want to focus.

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

    None of the solutions here worked for me. So, I came up with this:

    $('<form target="_blank" action="'+URL+'" method="post"></form>')
        .appendTo('body').submit();
    

    This creates a blank form that is submitted via post to the target URL. By applying target="_blank" we mimic calling window.open() with the side effect of maintaining focus on the new window.

    Edit: Vanilla JS version that also removes the form element afterwards:

    var f = document.createElement("form");
    f.setAttribute('method', 'post');
    f.setAttribute('action', URL);
    f.setAttribute('target', '_blank');
    document.getElementsByTagName('body')[0].appendChild(f);
    f.submit();
    f.parentNode.removeChild(f);
    
    0 讨论(0)
  • 2020-11-28 05:29

    I simply use this line of code here to refocus, it's the best thing that's worked for me so far:

    window.open('', 'NameOfTheOpenedWindow').focus();
    

    thanks to this answer [here](https://stackoverflow.com/a/30758862/10706668"Answer by dileepar")

    I also made a [jsfiddle](https://jsfiddle.net/Foxygaming9978/6gc38f5x/"jsfiddle popup refocus testing") for testing.

    I hope this helps

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