0

I have ah HTML file. CSS is at the beginning of the document, within tags. I need to get the class names of those classes that are located between comment tags

/* target >> */
.oneClass { ... }
.anotherOne { ... }    
/* target << */ 

I need to put the class names into an array

var myArray = new Array("oneClass","anotherOne " ...);

I can not really use anything server side scripting and is pretty much limited to basic JS. I assume I need some sort of regex, but this is way outside of my competency. Need some help.

1
  • 3
    Bah, it'll be ugly...basically need to get the script element, regex out that info (or sift thru it) Commented Jan 8, 2014 at 23:54

1 Answer 1

1

Maybe this will work, better than Regex since you won't be parsing the string, and get the actual rules interpreted by the browser.

var targetStyles = ((document.getElementsByTagName('style')[0].innerText).split('/* target >> */')[1]).split('/* target << */')[0];

var dimp = window.document.implementation.createHTMLDocument('');
var styleEl = dimp.createElement('style');
styleEl.innerHTML = targetStyles;
dimp.head.appendChild(styleEl);

var myArray = [], i, length = styleEl.sheet.rules.length;

for (i=0; i<length; i++) {
    myArray.push(styleEl.sheet.rules[i].selectorText);
}

//myArray contains what you are looking for.

JSFiddle with working demo.

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

Comments

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.