Window.name

获取/设置窗口的名称。

语法

string = window.name;
window.name = string;

示例

window.name = "lab_view";

备注

窗口的名字主要用于为超链接和表单设置目标(targets)。窗口不需要有名称。

在某些框架里(如,SessionVars 和 Dojo's dojox.io.windowName ,该属性也被用于作为 JSONP 的一个更安全的备选,来提供跨域通信(cross-domain messaging)。现代 web 应用应使用 postMessage API 进行敏感的跨域通信。

window.name 会调用 toString 将赋给它的值转换成对应的字符串表示。

(译注:此处似有不妥,私以为调用的应是 ToString 抽象方法。事实上,如果将一个Symbol类型的值赋给 window.name,会报 TypeError,而非调用 Symbol.toString() 转换成字符串后进行赋值。例如:

window.name = Symbol.for('foo'); // TypeError
window.name = Symbol.for('foo').toString(); // "Symbol(foo)"

具体可参见EMCA语言规范中Type Conversion一节。)

规范

规范 状态 说明
HTML Living Standard
Window.name
Living Standard
HTML5
Window.name
Recommendation

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
nameChrome Full support 1Edge Full support 12Firefox Full support 1
Notes
Full support 1
Notes
Notes From Firefox 82, if a new page from another domain is loaded into a tab, then window.name is set to the empty string (the original string is restored if the tab reverts back to the original page). This change prevents some cross-site attacks. See bug 444222.
IE Full support 4Opera Full support ≤12.1Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4
Notes
Full support 4
Notes
Notes From Firefox 82, if a new page from another domain is loaded into a tab, then window.name is set to the empty string (the original string is restored if the tab reverts back to the original page). This change prevents some cross-site attacks. See bug 444222.
Opera Android Full support ≤12.1Safari iOS Full support 1Samsung Internet Android Full support 1.0

Legend

Full support  
Full support
See implementation notes.
See implementation notes.