admin管理员组

文章数量:1287598

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)?

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)?

Share Improve this question edited Nov 15, 2011 at 12:44 Sai Kalyan Kumar Akshinthala 11.8k8 gold badges45 silver badges68 bronze badges asked Nov 15, 2011 at 12:41 loopinglooping 1,1613 gold badges11 silver badges20 bronze badges 1
  • See developer.mozilla/en/DOM/window.getComputedStyle for an explanation + exception (under defaultView) – Alex K. Commented Nov 15, 2011 at 12:53
Add a ment  | 

2 Answers 2

Reset to default 8

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 puted styles.

Simple if(window.getComputedStyle) would fail for iframes in Firefox 3.6 (according to article linked in ment 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 bination 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

本文标签: