JavaScript - 参考浏览器窗口的名称? [英] JavaScript - Reference Browser Window by name?

查看:181
本文介绍了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();

是否可以按名称访问和操作窗口( googleWindow )而不是变量?

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?

推荐答案

不。如果没有对窗口的引用,则无法通过名称或其他方式再次找到它。没有窗户的集合。

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];
}

现在, openWindow 将始终打开窗口,如果窗口已经存在,它将在该窗口中加载给定的URL并返回对该窗口的引用。现在你也可以实现 findWindow

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];
}

如果窗口存在,将返回窗口,或 undefined

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

你还应该有 closeWindow ,所以你不保留引用到你自己打开的窗口:

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];
    }
}




如果不是可能,给出Windows名称有什么意义?

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

该名称由浏览器在内部用于管理窗口。如果您使用相同的名称调用 window.open ,它将不会打开新窗口,而是将URL加载到先前打开的窗口中。还有一些内容,来自 MDN window.open()

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():


如果已存在名为strWindowName的窗口,则strUrl将加载到现有窗口中。在这种情况下,方法的返回值是现有窗口,并忽略strWindowFeatures。为strUrl提供空字符串是一种通过名称获取对打开窗口的引用而不更改窗口位置的方法。要在每次调用window.open()时打开一个新窗口,请为strWindowName使用特殊值_blank。

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.

这篇关于JavaScript - 参考浏览器窗口的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆