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