javascript document.defaultView 有什么意义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9183555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the point of document.defaultView?
提问by Dagg Nabbit
What's the point of document.defaultView
?
有什么意义document.defaultView
?
In browsers returns the window object associated with the document or null if none available.
在浏览器中,返回与文档关联的 window 对象,如果没有,则返回 null。
Code like the following (from PPK's site) makes use of document.defaultView
:
如下代码(来自PPK 的网站)使用了document.defaultView
:
function getStyle(el,styleProp)
{
var x = document.getElementById(el);
if (x.currentStyle)
var y = x.currentStyle[styleProp];
else if (window.getComputedStyle)
var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
return y;
}
Code like this can be found in other places, like David Mark's My Library. I'm not sure if people are just copying from PPK or some other source or coming up with this independently, but I don't understand it.
像这样的代码可以在其他地方找到,比如 David Mark 的My Library。我不确定人们是否只是从 PPK 或其他来源复制或独立提出这一点,但我不明白。
My question is, what is the point of using document.defaultView
in cases like this? Wouldn't it be easier to write this as follows:
我的问题是,document.defaultView
在这种情况下使用有什么意义?像下面这样写不是更容易吗:
function getStyle(element, styleProp) {
if (element === ''+element) element = document.getElementById(element);
return element.currentStyle ? element.currentStyle[styleProp] :
getComputedStyle(x,null).getPropertyValue(styleProp);
}
What does document.defaultView.getComputedStyle
do that window.getComputedStyle
or simply getComputedStyle
does not?
什么是document.defaultView.getComputedStyle
这样做window.getComputedStyle
或根本getComputedStyle
不这样做?
cwolves' answer got me thinking in the right direction. The original function is silly, missing the point of defaultView
. My proposal above is less silly, but also missing the point of defaultView
. Here's my new proposal:
狼队的回答让我想到了正确的方向。原来的函数很傻,没看懂defaultView
. 我上面的建议不那么愚蠢,但也没有抓住重点defaultView
。这是我的新提议:
function getStyle(element, styleProp) {
var view = element.ownerDocument && element.ownerDocument.defaultView ?
element.ownerDocument.defaultView : window;
return view.getComputedStyle ?
view.getComputedStyle(element,null).getPropertyValue(styleProp) :
element.currentStyle ?
element.currentStyle[styleProp] : null;
}
The element itself must be passed in, not the id. I think this is probably to be preferred anyway. This gets the document containing the node, and the window associated with it. It has a fallback to the current window's getComputedStyle
if ownerDocument
or defaultView
are broken (I vaguely remember getComputedStyle
being around before defaultView
). This is probably closer to the intended use of defaultView
.
必须传入元素本身,而不是 id。我认为无论如何这可能是首选。这将获取包含节点的文档以及与其关联的窗口。getComputedStyle
如果ownerDocument
或defaultView
已损坏,它会退回到当前窗口(我依稀记得getComputedStyle
之前在附近defaultView
)。这可能更接近于的预期用途defaultView
。
采纳答案by Mark Kahn
I'm not positive on this, but I imagine that it's the result of fixing a bug from either trying to run code on a detached document (i.e. something that exists in memory but is not in the page) or trying to run on a document in a different window (e.g. an iframe or a popup).
我对此并不乐观,但我认为这是修复错误的结果,该错误是尝试在分离的文档上运行代码(即存在于内存中但不在页面中的内容)或尝试在文档上运行在不同的窗口中(例如 iframe 或弹出窗口)。
According to your quote, when document.defaultView
is run on a document that is NOT the current document, you will get the associated window object, thus document.documentView.getComputedStyle !== getComputedStyle
since they are in different contexts.
根据您的报价,当document.defaultView
在不是当前文档的文档上运行时,您将获得关联的 window 对象,因此document.documentView.getComputedStyle !== getComputedStyle
因为它们处于不同的上下文中。
In short, I believe it's akin to document.window
which doesn't exist.
简而言之,我相信它类似于document.window
不存在的。
回答by Allasso
The OP asks the question, "What's the point of document.defaultView
", and the answer really doesn't have anything to do with getComputedStyle. The document.defaultView
property is simply a way of obtaining the window
object if one has a reference to the document
object contained in that window
. There are cases where the window
object you are seeking to reference (or defaultView
) is not in the same window scope as the code you are running.
OP 提出了“有什么意义document.defaultView
”的问题,答案实际上与 getComputedStyle 没有任何关系。如果拥有对包含在该对象中的对象的引用,则该document.defaultView
属性只是获取window
对象的一种方式。在某些情况下,您要引用的对象(或)与您正在运行的代码不在同一窗口范围内。document
window
window
defaultView
One example of this is if you have a reference to the document
object in an iframe, and wish to conveniently get a reference to the window
object of that iframe.
这方面的一个例子是,如果您有对document
iframe 中对象的引用,并且希望方便地获得window
对该 iframe 对象的引用。
Another case might be where you are running in privileged context in the browser scope (eg, chrome code in Firefox), and you happen to have a reference to the document
object of a tabbrowser, or another window.
另一种情况可能是您在浏览器范围内的特权上下文中运行(例如,Firefox 中的 chrome 代码),并且您碰巧拥有对选项卡浏览器document
或另一个窗口的对象的引用。
Or, as Dagg Nabbit points out, if in any of these cases you have a reference to an element within the window, you could access the parent window
of that element through element.ownerDocument.defaultView
或者,正如 Dagg Nabbit 指出的那样,如果在任何这些情况下您有对窗口内元素的引用,您可以通过访问该window
元素的父元素element.ownerDocument.defaultView
回答by BoltClock
回答by Oriol
According to MDN getComputedStyle
article,
In many code samples online,
getComputedStyle
is used from thedocument.defaultView
object.In nearly all cases, this is needless, as
getComputedStyle
exists on thewindow
object as well.It's likely the defaultView pattern was some combination of
- folks not wanting to write a spec for window and
- making an API that was also usable in Java.
However, there is a single casewhere the
defaultView
's method must be used: when using Firefox 3.6 to access framed styles.
在网上的很多代码示例中,
getComputedStyle
都是从document.defaultView
对象中使用的 。在几乎所有情况下,这都是不必要的,因为
getComputedStyle
在window
对象上也存在。defaultView 模式很可能是
- 人们不想为窗口编写规范和
- 制作一个也可以在 Java 中使用的 API。
然而,有一个案例,其中
defaultView
必须使用的方法:使用Firefox 3.6来访问框样式时。