Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

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

like image 704
looping Avatar asked Nov 15 '11 12:11

looping


People also ask

What is 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.

What is the difference between document and window in JavaScript what are some of the JavaScript frameworks and their uses?

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.

How do you set a computed style?

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 .

What computed styles?

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.


1 Answers

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

like image 161
WTK Avatar answered Oct 23 '22 10:10

WTK