1

I know that some would critic what I want, but I will ask my question.


I know that we can't get the style of an element if their css weren't formated inline. So I want a regex (because I am not very good to make one) that will get all the style tags and will apply a regex for their innerHTML to make an array like this one :

css["myDiv"]["color"] -> "red"

Without using getComputedStyle because of the browser incompatibility.

2
  • See stackoverflow.com/questions/1732348/… Commented Jun 6, 2012 at 20:44
  • 1
    You're 30x better off using a combination of getComputedStyle and pre IE9's currentStyle. Commented Jun 6, 2012 at 20:55

1 Answer 1

3

There is no way you achieve anything remotely close to this using regex. CSS rules are applied using a complicated algorithm and rules may be inherited, prioritized and overridden in various ways. It is no coincidence that it took the major browsers years to come up with a compliant implementation.

You should exercise exactly that implementation instead. The tool you need is JavaScript and the API you need is the DOM. For each node in the DOM there is a style object which contains all the applied styles and which has a format very close to what you are trying to generate. You can access this via getComputedStyle and currentStyle.

Sign up to request clarification or add additional context in comments.

2 Comments

can you add the cross-browser combination of getComputedStyle and currentStyle like snuffleupagus said and I will check your answer as accepted!
I will give you a link to a useful page that has the info and the code you need: quirksmode.org/dom/getstyles.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.