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)
?
getComputedStyle() The Window. getComputedStyle() method returns an object containing the values of all CSS properties of an element, after applying active stylesheets and resolving any basic computation those values may contain.
All the tags, elements with attributes in HTML are part of the document. Global objects, functions, and variables of JavaScript are members of the window object. The window is part of BOM, not DOM. Properties of the window object cannot be accessed by the document object.
To set or copy JavaScript computed style from one element to another, we can loop through each style and call the setProperty method to set the styles on the target element. to add a div and a section element. We define the copyNode function that takes the sourceNode and targetNode .
A “computed style” is all the styles that applies to the element, even if there's no CSS specified for that element. For example, consider the color of a element, the element itself may not have a CSS color spec, but it inherit styles from parent element, or from browser's initial value for that property.
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.).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With