9

When you create a new browser window, you pass it a name like this:

myWindow = window.open('http://www.google.com', "googleWindow");

Later you can access the window from the variable you saved it as:

myWindow.close();

Is it possible to access and manipulate a window by it's name (googleWindow) instead of the variable?

If it is not possible, what is the point giving windows names?

Jake Wilson
  • 88,616
  • 93
  • 252
  • 370

5 Answers5

20

No. Without a reference to the window, you can't find it again, by name or otherwise. There is no collection of windows.

UPDATE: Here's how you could do it yourself:

var windows = {};
function openWindow(url, name, features) {
    windows[name] = window.open(url, name, features);
    return windows[name];
}

Now, openWindow will always open the window, and if the window already exists, it will load the given URL in that window and return a reference to that window. Now you can also implement findWindow:

function findWindow(name) {
    return windows[name];
}

Which will return the window if it exists, or undefined.

You should also have closeWindow, so you don't keep references to windows that you opened yourself:

function closeWindow(name) {
    var window = windows[name];
    if(window) {
        window.close();
        delete windows[name];
    }
}

If it is not possible, what is the point giving windows names?

The name is used internally by the browser to manage windows. If you call window.open with the same name, it won't open a new window but instead load the URL into the previously opened window. There are a few more things, from MDN window.open():

If a window with the name strWindowName already exists, then strUrl is loaded into the existing window. In this case the return value of the method is the existing window and strWindowFeatures is ignored. Providing an empty string for strUrl is a way to get a reference to an open window by its name without changing the window's location. To open a new window on every call of window.open(), use the special value _blank for strWindowName.

Linus Thiel
  • 38,647
  • 9
  • 109
  • 104
  • This may be fine for windows but it does not work on my linux system...not sure if it a Microsoft windows reference or not, but sometimes what happens on my Linux machine also happens on Macs... – CrandellWS Aug 17 '14 at 12:37
  • Starting with Google Chrome 41, this method has a bad side effect: whenever you execute window.open(), the already open window will have focus. There is currently no workaround that I could find to prevent this behavior, which is giving me headaches. I only want to have reference to that window, I don't want that it gets focus. calling window.open() with empty url does not work. – schlingel Apr 07 '15 at 14:01
  • I have had these issues with Chrome, too. I got around mine (which were all related the actual end-user operation, e.g. click, shift+click, etc) by performing the window.open in a short timer, thus disassociating it from the user operation. – ACProctor Nov 04 '21 at 13:16
17

Linus G Thiel says that you cannot do this in javascript. Oddly enough, his answer lists an excerpt from MDN that sounds like it tells how to do this. The line was:

 "Providing an empty string for strUrl is a way to get a reference to
  an open window by its name without changing the window's location."

I tried this and it works for me.

 winref = window.open('', 'thatname', '', true);
 winref.close();

However, this may only work if you opened the window from your page. And if that's true, then it's kind of pointless to do a window.open just to get the reference. You probably already have the reference, in that case.

Mark Goldfain
  • 731
  • 2
  • 8
  • 24
  • 2
    It's actually not based on who opened it, it's based on what origin or domains the pages share. – aredridel Oct 22 '13 at 16:19
  • You can clear the contents with `winref.document.close();` and add new content with `winref.document.write('New content');` – CrandellWS Aug 17 '14 at 12:34
3

Mark Goldfain's solution no longer works as written as of 9/8/2015

As per this w3 specification,

If the first argument is the empty string, then the url argument must be interpreted as "about:blank".

I believe this is a difference between HTML4 and HTML5.

IE and Chrome have updated this behavior to match this specification, while Mark's solution still works on FF (though I imagine that they'll fix this soon). A few weeks ago this worked on all major browsers.

My particular problem involved window control while navigating, where the chat window opening is black boxed as well as most of the code on the page - redefining window.open was right out. My solution involved calling the blank window with the reference before calling the function which called the chat window. When the user navigated away from the page, I was able to rely on the fact that windows other than the original parent are not allowed to modify the child window, and so I was able to use Mark Goldfain's solution unchanged.

3

The solution provided by Mark Goldfain can be edited to work with the new browsers, at least to open a window and keep a reference to it between page refresh.

var winref = window.open('', 'MyWindowName', '', true);
if(winref.location.href === 'about:blank'){
    winref.location.href = 'http://example.com';
}

or in function format

function openOnce(url, target){
    // open a blank "target" window
    // or get the reference to the existing "target" window
    var winref = window.open('', target, '', true);

    // if the "target" window was just opened, change its url
    if(winref.location.href === 'about:blank'){
        winref.location.href = url;
    }
    return winref;
}
openOnce('http://example.com', 'MyWindowName');
Community
  • 1
  • 1
Timo Huovinen
  • 53,325
  • 33
  • 152
  • 143
0

I ended up using the following:

var newwindows = {};
function popitup(url, nm) {
  if ((newwindows[nm] == null) || (newwindows[nm].closed)) {
    newwindows[nm] = window.open(url, nm, 'width=1200,height=650,scrollbars=yes,resizable=yes');
  }
  newwindows[nm].focus();
}

then referenced using:

     <button type="button" onclick="popitup('url/link.aspx?a=bc',this.value)" value="uniqueName">New</button>
Jaynesify
  • 19
  • 4
  • Why not simplify the logic and put `newwindows[nm].focus();` outside of the if logic altogether? Seems redundant to execute the same action in two different logic paths, when the result is the same action either way. – GoldBishop Feb 02 '18 at 19:32