Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript - Reference Browser Window by name? [duplicate]

Tags:

javascript

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?

like image 303
Jake Wilson Avatar asked Feb 20 '12 16:02

Jake Wilson


People also ask

Can I pass a JavaScript variable to another browser Window?

How do I pass data from one page to another in JavaScript? There are two ways to pass variables between web pages. The first method is to use sessionStorage, or localStorage. The second method is to use a query string with the URL.

How to get the popup Window name in JavaScript?

The syntax to open a popup is: window. open(url, name, params) : url. An URL to load into the new window.

How do you pass a variable open in Windows?

Passing variables between the windows (if your windows are on the same domain) can be easily done via: Cookies. localStorage. Just make sure your browser supports localStorage, and do the variable maintenance right (add/delete/remove) to keep localStorage clean.

How do I find out my windows name?

Press the Windows logo key + X to see a list of commands and options. Click System. The computer name appears under Computer name, domain, and workgroup settings.


2 Answers

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.

like image 147
Linus Thiel Avatar answered Sep 29 '22 15:09

Linus Thiel


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.

like image 38
Mark Goldfain Avatar answered Sep 29 '22 16:09

Mark Goldfain