Dev.Opera - Follow the standards, break the rulesDev.Opera - Follow the standards, break the rules

Login

Lost password?

Forums » General Discussions

General DevOpera discussion group

Note: You need to login to post in the forums. if you don't have an account you first need to sign up.

By Matthijn anchor Wednesday, 2. April 2008, 17:02:59

avatar

get Fontfamily from stylesheet

Hello,

When I have for example this stylesheet:

h1 {
font-family:Freestyle,Arial,Verdana,sans-serif;


And want to get the fontFamily with javascript from an h1 of that page i use getComputedStyle for non IE browsers, and currentStyle for IE browsers).

Every browser gives me the full font family, example:


Freestyle,Arial,Verdana,sans-serif


But opera only gives me one font, and that is the font which it has chosen to render the page with (example)


Arial


How can I get the full fontFamily with javascript in a way that is works with Opera too? obj.style.fontFamily is not an option because that only shows the inline styles, and I want the style of an stylesheet.

Who can help me? Thank allot.

By mrhoo anchor Tuesday, 15. April 2008, 14:48:47

avatarThis could take a lot of code.

You can loop through all the document.styleSheets, and if the font-family was set as a rule in a declaration with your element's id in the selector, you can look for the id in the selectorText and parse the rule's cssText. If the font properties were set using the font: shortcut you will have to examine the rule closely and not return strings like 'large' or 'italic'.

If the element's css was not set specifically for its id you need to do a lot more work, and figure out all the elements that are above it in the cascade, and then figure out which of those elements has more specificity, and would trump a higher container with less specificity.

If an element is in a named container that has a specified font-family, and the element doesn't have its own specific css assignment, it can inherit the ancestor's font family.

It is possible to account for all this, but it will take a lot of code.

I have always found that it is enough to know what is actually being used, rather than what might have been..

Moderators: jax | malware | mcx | operadev