Javascript Bring window to front if already open in window.open?

后端 未结 8 634
别那么骄傲
别那么骄傲 2020-11-30 07:35

If you open a window like:

window.open (\"url\",\"winName\",\"location=0,width=300,height=214\");

If winName is already open i

相关标签:
8条回答
  • 2020-11-30 07:45

    Closing the window first, does the trick for me:

    window.open('', 'mypopup').close();
    setTimeout(function() {
        window.open('', 'mypopup').focus();
    }, 500);
    
    0 讨论(0)
  • 2020-11-30 07:47

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

    The window.open() method returns an object that represents the new window. You just need to window.focus() it:

    var w = window.open ("url","winName","location=0,width=300,height=214");
    w.focus();
    

    Or simply:

    window.open("url","winName","location=0,width=300,height=214").focus();
    
    0 讨论(0)
  • 2020-11-30 07:49

    window.focus() applied to the window in question should do the trick.

    0 讨论(0)
  • 2020-11-30 07:49

    I fixed this by adding

    onclick="myWin = window.open('','winName','location=0,width=300,height=214'); myWin.focus()"
    

    to the html element(button) and then change the URL via JS.

    0 讨论(0)
  • 2020-11-30 07:53

    You can use jQuery :

    myPopup = window.open(url, "Title","menubar=no, status=no, scrollbars=no, menubar=no, width=800, height=800");
    $(myPopup).focus();
    
    0 讨论(0)
  • 2020-11-30 07:55

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

    Be careful, because when you open a new window, the opener window might still have some code to be executed, maybe some of this code gives it the focus. You would see how your new window opens in the front and suddenly goes to the back, so, it is a great idea in these cases, to set a timeout in order to give the focus to the new window a bit later on, when all the javascript in the opener window is executed, you can do it this way:

        setTimeout(function(){window.focus();},1000);
    

    Being 1000 the amount of miliseconds to wait, and window the name of the opened window. You could also use this code in the opened window in the body onload for example.

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