简体   繁体   中英

what's the difference bewteen document.defaultView.getComputedStyle and window.getComputedStyle

when get a element's style, we always use

if(document.defaultView && document.defaultView.getComputedStyle) to check whether the browser support the method or not.

why not use if(window.getComputedStyle) ?

So in short, the reason why we use document.defaultView && document.defaultView.getComputedStyle is that we want a cross-browser working-on-every-element method of checking whenever it supports fetching computed styles.

Simple if(window.getComputedStyle) would fail for iframes in Firefox 3.6 (according to article linked in comment by Alex K.).

According to the MDN defaultView is no longer required

In many code samples, getComputedStyle is used from the document.defaultView object. In nearly all cases, this is needless, as getComputedStyle exists on the window object as well. It's likely the defaultView pattern was a combination of folks not wanting to write a testing spec for window and making an API that was also usable in Java.

There was a bug in Firefox 3.6 (2010/2011) that needed defaultView fix

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM