Amit
Amit

Reputation: 3990

Firefox 5 gives all CSS when running getComputedStyle

Using firefox 5 when i run

window.getComputedStyle(document.getElementsByTagName("img")[0], null);

I get the complete css file, instead of the styles applied onto the "img" tag.

I ran this on https://developer.mozilla.org/en/DOM/window.getComputedStyle

Anyone knows of a workaround?

Upvotes: 0

Views: 570

Answers (3)

zupa
zupa

Reputation: 13402

I know this is an older post, but for anyone landing here.

Basic idea: you need to call the getPropertyValue() method on the object returned by window.getComputedStyle().

See this fiddle: http://jsfiddle.net/zupa/jyyt9/

MDN states you don't need to call document.defaultView.getComputedStyle() but window.getComputedStyle

Note that window.getComputedStyle() returns used values not computed values. (See previous link.)

Compatibility tables: MDN, quirksmode

Upvotes: 1

Augustus Kling
Augustus Kling

Reputation: 3333

It should give you an object of type ComputedCSSStyleDeclaration that includes all styles that have been set. This includes all possible styles and not only those styles that have been manipulated by you in some way.

In order to get a specific rule, use for example:

window.getComputedStyle(document.getElementsByTagName("img")[0], null)['borderLeftColor'];

This gives the left border color without distinguishing how the value as specified / calculated.

To get a list of the available entries print the object to Firebug's console:

console.dir(window.getComputedStyle(document.getElementsByTagName("img")[0], null));

Upvotes: 1

Tushar Ahirrao
Tushar Ahirrao

Reputation: 13115

Use this :

document.defaultView.getComputedStyle(document.getElementsByTagName("img")[0], "");

Upvotes: 0

Related Questions