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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:51:11  来源:igfitidea点击:

What's the point of document.defaultView?

javascriptdom

提问by Dagg Nabbit

What's the point of document.defaultView?

有什么意义document.defaultView

MDN says:

MDN 说

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.defaultViewin 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.getComputedStyledo that window.getComputedStyleor simply getComputedStyledoes 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 getComputedStyleif ownerDocumentor defaultVieware broken (I vaguely remember getComputedStylebeing around before defaultView). This is probably closer to the intended use of defaultView.

必须传入元素本身,而不是 id。我认为无论如何这可能是首选。这将获取包含节点的文档以及与其关联的窗口。getComputedStyle如果ownerDocumentdefaultView已损坏,它会退回到当前窗口(我依稀记得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.defaultViewis run on a document that is NOT the current document, you will get the associated window object, thus document.documentView.getComputedStyle !== getComputedStylesince they are in different contexts.

根据您的报价,当document.defaultView在不是当前文档的文档上运行时,您将获得关联的 window 对象,因此document.documentView.getComputedStyle !== getComputedStyle因为它们处于不同的上下文中。

In short, I believe it's akin to document.windowwhich 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.defaultViewproperty is simply a way of obtaining the windowobject if one has a reference to the documentobject contained in that window. There are cases where the windowobject 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对象的一种方式。在某些情况下,您要引用的对象(或)与您正在运行的代码不在同一窗口范围内。documentwindowwindowdefaultView

One example of this is if you have a reference to the documentobject in an iframe, and wish to conveniently get a reference to the windowobject of that iframe.

这方面的一个例子是,如果您有对documentiframe 中对象的引用,并且希望方便地获得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 documentobject 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 windowof that element through element.ownerDocument.defaultView

或者,正如 Dagg Nabbit 指出的那样,如果在任何这些情况下您有对窗口内元素的引用,您可以通过访问该window元素的父元素element.ownerDocument.defaultView

回答by BoltClock

It is simply an abstraction as far as I can tell, just in case any user agents pop up with a DOM implementation, but don't provide a view in the form of a window. See Viewsin DOM level 2.

据我所知,这只是一个抽象,以防万一任何用户代理弹出一个 DOM 实现,但不要以窗口的形式提供视图。请参阅DOM 级别 2 中的视图

回答by Oriol

According to MDN getComputedStylearticle,

根据MDNgetComputedStyle文章

In many code samples online, getComputedStyleis used from the document.defaultViewobject.

In nearly all cases, this is needless, as getComputedStyleexists on the windowobject as well.

It's likely the defaultView pattern was some combination of

  1. folks not wanting to write a spec for window and
  2. 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对象中使用的 。

在几乎所有情况下,这都是不必要的,因为 getComputedStylewindow对象上也存在。

defaultView 模式很可能是

  1. 人们不想为窗口编写规范和
  2. 制作一个也可以在 Java 中使用的 API。

然而,有一个案例,其中defaultView必须使用的方法:使用Firefox 3.6来访问框样式时。