How can I get CSS comments using jQuery?

I am wondering how I can read CSS comments from a linked stylesheet.

I have this CSS sample loaded via:

<link rel="stylesheet" type="text/css" media="all" href="test.css" /> 

 #test1{ border:1px solid #000; } #test2{ border:1px solid #000; } #test3{/* sample comment text I'm trying to read */} 

I am testing this in FF3. The following javascript reads the rules but does not read the comments in #test3 .

 window.onload = function(){ s=document.styleSheets; for(i=0;i < s[0].cssRules.length;i++){ alert(s[0].cssRules[i].cssText); } } 
+3
source share
5 answers

You can get the contents of the stylesheet and use regex to parse the comments. This example uses jQuery to get styles text and a regular expression to search for comments:

 jQuery.get("test.css", null, function(data) { var comments = data.match(/\/\*.*\*\//g); for each (var c in comments) alert(c); }); 

You can also find links to stylesheets using selectors.

+7
source

Comments will almost always be ignored by the interpreter and therefore will not be available.

+4
source

You can access the CSS file using an AJAX request, and then parse the results themselves looking at the comments. The interpreter will not interfere then.

As long as CSS is in the same domain as the page, this will work well.

+4
source

You can’t have that whole comment.

+2
source

You cannot read the JavaScript CSS file, just check the results in the DOM. One possible way would be to use a built-in style sheet where you can request the text content of a style tag through the DOM interface. Of course, you need to parse the content for yourself.

0
source

All Articles