Access a window by window name

后端 未结 7 1137
悲哀的现实
悲哀的现实 2020-11-29 07:08

If I open a window using

window.open(\'myurl.html\', \'windowname\', \'width=100,height=100\');

How do I refer to the new window (from the

相关标签:
7条回答
  • 2020-11-29 07:34

    afaik there's no way like windows['windowname']. The 'windowname' assigned in window.open() can be addressed as a target in <a target="windowname" [...] >

    0 讨论(0)
  • 2020-11-29 07:36

    Try open that window with the name, but URL is '' again, to check if it's a blank window or not. If it's in open, then you will get the window; if not, a new window open, and you need close it. Add the children in localStorage will help to prevent to open the new blank window.

    Please check my code in https://github.com/goldentom66/ParentChildWindow

    0 讨论(0)
  • 2020-11-29 07:42

    Petr is correct:

    var w = window.open("", "nameofwindow");
    

    works in all browsers, I am using it to retrieve the reference to the window object previously opened by a different page. The only problem is the initial opening of the page, if the popup does not exist, you will get a new window with a blank page.

    I tried invoking a Javascript function inside the context of the other document in order to check whether I opened a new window or retrieved the already active page. If the check fails, I just invoke window.open again to actually load my popup content:

    var w = window.open("http://mydomain.com/myPopup", "nameofwindow");
    

    Hope that helps.

    0 讨论(0)
  • 2020-11-29 07:43

    It is not possible. The windowName is just to be used in target="..." of links/forms or to use the same name again in another window.open call to open a new url in that window.

    0 讨论(0)
  • 2020-11-29 07:44

    In firefox (might work in other browsers too, but now it's not my concern) I was able to reference one window accross multiple page loads with

    var w = window.open("", "nameofwindow");
    

    This opens new window if it doesn't exist and return reference to existing window if it does exist without changing contents of the window.

    With jQuery I was then able to append new content, to make quick collection of interresting links like this

    $('body', w.document).append(link_tag);
    
    0 讨论(0)
  • 2020-11-29 07:52

    Sorry I am posting late, but if you still have the other window open, and they are on the same domain, you can run, on the first window:

    function getReference(w) {
       console.log('Hello from', w);
    }
    

    And on the second window:

    window.opener.getReference(window);
    
    0 讨论(0)
提交回复
热议问题