I can get window.document but how can I get document.window? I need to know how to do it in all browsers.
我可以获得window.document但是如何获得document.window?我需要知道如何在所有浏览器中执行此操作。
6 个解决方案
#1
-3
The Window object is the top level object in the JavaScript hierarchy, so just refer to it as window
Window对象是JavaScript层次结构中的*对象,因此只需将其称为窗口即可
Edit: Original answer before Promote JS effort. JavaScript technologies overview on Mozilla Developer Network says:
编辑:促进JS努力之前的原始答案。 Mozilla Developer Network上的JavaScript技术概述说:
In a browser environment, this global object is the window object.
在浏览器环境中,此全局对象是窗口对象。
Edit 2: After reading the author's comment to his question (and getting downvotes), this seems to be related to the iframe's document window. Take a look at window.parent and window.top and maybe compare them to infer your document window.
编辑2:在阅读作者对他的问题的评论(并获得downvotes)后,这似乎与iframe的文档窗口有关。看一下window.parent和window.top,然后比较它们来推断你的文档窗口。
if (window.parent != window.top) {
// we're deeper than one down
}
#2
79
You can go with document.defaultView
if you’re sure its a window and its okay to skip Microsoft browsers before IE 9.
你可以使用document.defaultView,如果你确定它是一个窗口,它可以在IE 9之前跳过Microsoft浏览器。
#3
17
A cross browser solution is complicated, here's how dojo does it (from window.js::get()):
跨浏览器解决方案很复杂,这里是dojo的做法(来自window.js :: get()):
// In some IE versions (at least 6.0), document.parentWindow does not return a
// reference to the real window object (maybe a copy), so we must fix it as well
// We use IE specific execScript to attach the real window reference to
// document._parentWindow for later use
if(has("ie") && window !== document.parentWindow){
/*
In IE 6, only the variable "window" can be used to connect events (others
may be only copies).
*/
doc.parentWindow.execScript("document._parentWindow = window;", "Javascript");
//to prevent memory leak, unset it after use
//another possibility is to add an onUnload handler which seems overkill to me (liucougar)
var win = doc._parentWindow;
doc._parentWindow = null;
return win; // Window
}
return doc.parentWindow || doc.defaultView; // Window
has("ie") returns true for IE (and false otherwise)
has(“ie”)为IE返回true(否则返回false)
#4
3
Well, this is the solution I went with. It works, but I hate it.
嗯,这是我选择的解决方案。它有效,但我讨厌它。
getScope : function(element) {
var iframes = top.$$('iframe');
var iframe = iframes.find(function(element, i) {
return top[i.id] ? top[i.id].document == element.ownerDocument : false;
}.bind(this, element));
return iframe ? top[iframe.id] : top;
}
#5
1
I opted to inject the DOCUMENT
token from @angular/platform-browser
:
我选择从@ angular / platform-browser注入DOCUMENT令牌:
import { DOCUMENT } from '@angular/platform-browser'
and then access the parent:
然后访问父级:
constructor(@Inject(DOCUMENT) private document: any) {
}
public ngOnInit() {
// this.document.defaultView || this.document.parentWindow;
}
#6
0
first off let's be clear. this sort of thing is often necessary when you are working with frames, iframes, and multiple windows, and so "the window is just the global object" is an unsatisfying answer if all you have a handle to is a document from another window than the one you are in.
首先让我们清楚。当您使用框架,iframe和多个窗口时,这种事情通常是必要的,因此“窗口只是全局对象”是一个令人不满意的答案,如果你有一个句柄是来自另一个窗口的文件而不是一个你在。
second, unfortunately there is no direct way of getting at the window object. there are indirect ways.
第二,遗憾的是没有直接获取窗口对象的方法。有间接的方式。
the primary mechanism to use is window.name. when creating a window or a frame from some parent window, you can usually give it a unique name. any scripts inside that window can get at window.name. any scripts outside the window can get at the window.name of all its child windows.
要使用的主要机制是window.name。从某个父窗口创建窗口或框架时,通常可以为其指定一个唯一的名称。该窗口中的任何脚本都可以在window.name中获得。窗口外的任何脚本都可以获取其所有子窗口的window.name。
to get more specific than that requires more info about the specific situation. however in any situation where the child scripts can communicate with parent scripts or vice versa, they can always identify each other by name, and this is usually enough.
要获得更具体的信息,需要有关具体情况的更多信息。但是,在子脚本可以与父脚本通信的任何情况下,反之亦然,它们总是可以通过名称相互识别,这通常就足够了。
#1
-3
The Window object is the top level object in the JavaScript hierarchy, so just refer to it as window
Window对象是JavaScript层次结构中的*对象,因此只需将其称为窗口即可
Edit: Original answer before Promote JS effort. JavaScript technologies overview on Mozilla Developer Network says:
编辑:促进JS努力之前的原始答案。 Mozilla Developer Network上的JavaScript技术概述说:
In a browser environment, this global object is the window object.
在浏览器环境中,此全局对象是窗口对象。
Edit 2: After reading the author's comment to his question (and getting downvotes), this seems to be related to the iframe's document window. Take a look at window.parent and window.top and maybe compare them to infer your document window.
编辑2:在阅读作者对他的问题的评论(并获得downvotes)后,这似乎与iframe的文档窗口有关。看一下window.parent和window.top,然后比较它们来推断你的文档窗口。
if (window.parent != window.top) {
// we're deeper than one down
}
#2
79
You can go with document.defaultView
if you’re sure its a window and its okay to skip Microsoft browsers before IE 9.
你可以使用document.defaultView,如果你确定它是一个窗口,它可以在IE 9之前跳过Microsoft浏览器。
#3
17
A cross browser solution is complicated, here's how dojo does it (from window.js::get()):
跨浏览器解决方案很复杂,这里是dojo的做法(来自window.js :: get()):
// In some IE versions (at least 6.0), document.parentWindow does not return a
// reference to the real window object (maybe a copy), so we must fix it as well
// We use IE specific execScript to attach the real window reference to
// document._parentWindow for later use
if(has("ie") && window !== document.parentWindow){
/*
In IE 6, only the variable "window" can be used to connect events (others
may be only copies).
*/
doc.parentWindow.execScript("document._parentWindow = window;", "Javascript");
//to prevent memory leak, unset it after use
//another possibility is to add an onUnload handler which seems overkill to me (liucougar)
var win = doc._parentWindow;
doc._parentWindow = null;
return win; // Window
}
return doc.parentWindow || doc.defaultView; // Window
has("ie") returns true for IE (and false otherwise)
has(“ie”)为IE返回true(否则返回false)
#4
3
Well, this is the solution I went with. It works, but I hate it.
嗯,这是我选择的解决方案。它有效,但我讨厌它。
getScope : function(element) {
var iframes = top.$$('iframe');
var iframe = iframes.find(function(element, i) {
return top[i.id] ? top[i.id].document == element.ownerDocument : false;
}.bind(this, element));
return iframe ? top[iframe.id] : top;
}
#5
1
I opted to inject the DOCUMENT
token from @angular/platform-browser
:
我选择从@ angular / platform-browser注入DOCUMENT令牌:
import { DOCUMENT } from '@angular/platform-browser'
and then access the parent:
然后访问父级:
constructor(@Inject(DOCUMENT) private document: any) {
}
public ngOnInit() {
// this.document.defaultView || this.document.parentWindow;
}
#6
0
first off let's be clear. this sort of thing is often necessary when you are working with frames, iframes, and multiple windows, and so "the window is just the global object" is an unsatisfying answer if all you have a handle to is a document from another window than the one you are in.
首先让我们清楚。当您使用框架,iframe和多个窗口时,这种事情通常是必要的,因此“窗口只是全局对象”是一个令人不满意的答案,如果你有一个句柄是来自另一个窗口的文件而不是一个你在。
second, unfortunately there is no direct way of getting at the window object. there are indirect ways.
第二,遗憾的是没有直接获取窗口对象的方法。有间接的方式。
the primary mechanism to use is window.name. when creating a window or a frame from some parent window, you can usually give it a unique name. any scripts inside that window can get at window.name. any scripts outside the window can get at the window.name of all its child windows.
要使用的主要机制是window.name。从某个父窗口创建窗口或框架时,通常可以为其指定一个唯一的名称。该窗口中的任何脚本都可以在window.name中获得。窗口外的任何脚本都可以获取其所有子窗口的window.name。
to get more specific than that requires more info about the specific situation. however in any situation where the child scripts can communicate with parent scripts or vice versa, they can always identify each other by name, and this is usually enough.
要获得更具体的信息,需要有关具体情况的更多信息。但是,在子脚本可以与父脚本通信的任何情况下,反之亦然,它们总是可以通过名称相互识别,这通常就足够了。