Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieve all colors from CSS file and store into array 2

Status
Not open for further replies.

Sleidia

Technical User
Joined
May 4, 2001
Messages
1,284
Location
FR

Hi,

Using Javascript, what would be the easiest way to retrieve all colors from a CSS file and store them all into an array?

Thanks! :)
 

Hi again,

Yeah, the CSS file should be linked into the same document.
I wondered if there was a simple DOM method for catching all colors from a styl sheet. If not, instead of using a complicated javascript, I will use PHP.

Thanks again for your precious help.
 
Hi

Well, I played only a few time with this. The following works in FireFox and not works in Explorer. ( A search for "javascript cssrules" will probably list some documents about their incompatibility. )

Its beauty and ugliness in the same time is that the values are already translated in [tt]rgb()[/tt] format.
Code:
var color=[]
var selector=['color','backgroundColor','borderTopColor','borderBottomColor','borderLeftColor','borderRightColor']

for (var i=0,l=document.styleSheets.length;i<3;i++) {
  for (var j=0,m=document.styleSheets[i].cssRules.length;j<m;j++) {
    if (document.styleSheets[i].cssRules[j].type==CSSRule.STYLE_RULE) {
      for (var e=0,n=selector.length;e<n;e++) {
        if (document.styleSheets[i].cssRules[j].style[selector[e]] && document.styleSheets[i].cssRules[j].style[selector[e]]!='transparent') {
          color.push(document.styleSheets[i].cssRules[j].style[selector[e]])
        }
      }
    }
  }
}

alert(color.join('\n'))

Feherke.
 
Thanks!

But I will use your PHP version because less risky.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top