• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 716
  • Last Modified:

Stylesheet object (This may be difficult)?

O.K. here is the issue, I have a style shhet include in my page.  

<% script language="vbscript">
document.createstylesheet("MyStyleSheet.css")
</script>

Now lets the the style sheet has the following text:

body{
  font-size:12px;
}
.td{
  font-size:10px
}
#hello{
  fonr-size:8px
}

I need to loop through the stylesheet object (or something similar) and for each class (i.e. body, .td), I need to retrieve the style properties so that I can Dynamically populate specific variables.

Why??  because I can't figure this out (driving me mad), and I don't want to read the file via the filesystem object.

Thanks in advance.
~Turbo

(If I feel the need I can award extra point if someone is overly helpful in solving this problem.),

requirements:
1. MUST be VBScript, no JavaScript.
Thats it....
0
turbosig
Asked:
turbosig
1 Solution
 
raizonCommented:
can you show us some code as to what you mean by dynamically populating variables.  I'm not sure exactly why you just don't include it in the <head> or by
<link rel="stylesheet" src="myCss.Css" type="text/css">
0
 
turbosigAuthor Commented:
<---
I'm not sure exactly why you just don't include it in the <head> or by <link rel="stylesheet" src="myCss.Css" type="text/css">
--->

Actually:

<% script language="vbscript">
document.createstylesheet("MyStyleSheet.css")
</script>

does the same thing (it's just what we use), but what I really want to know is how to get each tag, class and unique Id and their styles from the stylesheet object

I want to ba able to view an asp page that shows examples of all the styles, (let's say one of our developers add's a new style that wasn't there before.

MOSTLY what I want ot do is learn how to use the style sheet object and be able to loop through the collection (I guess this last sentence sums it up).  I know how to include a style sheet, just want to use the object / collection, but have never used or nor do I quite know how?
0
 
turbosigAuthor Commented:
Maybe this is more clear,

I want to do something like the following (i know it is wrong)

   
     set col = document.styleSheets(0)
     for each (class, tag unique Id) in col
           ...List col.styles
     next

This is what I am trying to achieve.  Of course substitute in proper VBScript, etc. to actually make it work, but that is the textual representation of what I want.

I hope this is more clear???

Got this from MSDN....

For Internet Explorer 4.0, a further example is the document.styleSheets object which is an array/collection of objects, one per <STYLE> or <LINK> tag present in the document. Each of the collection elements has a property rules, which is an array/collection of the individual CSS rules.

Also..

IHTMLStyleSheetsCollection Interface
This interface provides the ability to obtain the number of styleSheets in the collection, and to iterate through the styleSheets collection of the STYLE element.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Michel SakrCommented:
you want it on the client side or server side? on server side it's not possible without fso..
0
 
turbosigAuthor Commented:
Silvers5,

With fso are you not infact just reading the contents and not using Collections?
0
 
raizonCommented:
Read the contents into a variable and then use the split along with the mid function to put it into an array.
0
 
turbosigAuthor Commented:
You know what, I am trying to use fso, but I don't get everyline returned?
0
 
turbosigAuthor Commented:
Better yet, instead of all this stuff, I REALLY just want to see some samples using the stylesheet object to loop through the tags, classes, and unigue id's and get their style.  

That is the simple explaination.
0
 
raizonCommented:
for the FSO method give this a try.

Format your stylesheet where each style is on a single line

body{font-size:12px;}
.td{font-size:10px}
#hello{font-size:8px}

use ReadAll to read it all into a single variable

<%
myCss = fso.ReadAll(path to file)
%>


then split to split the variable into an array.

<%
myCssArray() = split(myCss, vbCrLf, 1)
%>


then use the left function to get the element name

<%
For i=0 to ubound(myCssArray(i))
  Response.Write Left(myCssArray(i),instr(myCssArray(i),"{"))
Next
%>

Raizon
0
 
turbosigAuthor Commented:
Yea, actually that is sorta what I am doing right now.

I am not looking perse for a WAY TO DO IT.  I am looking for a WAY TO DO IT using the stylesheet object.
0
 
DeathwishCommented:
First I don't think you can use the stylesheet object on the server side. The quotes you mention are found in the DHTML section and IHTML which those are clientside or IE things. Second, the stylesheet colloection that you mention in the MSDN quote is simpley a collection of each stylesheet. For example lets say you have the following:

<STYLE id=1> some style code here....</STYLE>
<STYLE id=2> some style code here....</STYLE>
<STYLE id=3> some style code here....</STYLE>
<LINK type="text/css" src="mystyle.css">

The stylesheet collection would be all 4 of these. You can then iterate through each. See this in the MSDN:

http://msdn.microsoft.com/workshop/author/dhtml/reference/collections/styleSheets.asp?frame=true

You can then loop through and get their properties and attributes. In other words I could say something like this:

colStyles = document.styleSheets
alert(colStyles(0).id)

It would give me the first style object and its id in an alert statement. The createstylesheet method simply creates a style object. From there you can dynamically change or add a rule or class if you know what it is called or its ordinal position.

Anyway, to get back to your question the closest you will come to is this:

http://msdn.microsoft.com/workshop/author/dhtml/reference/collections/rules.asp?frame=true

Here is a little test script and page:


<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<STYLE id=test1>
.test {
     font-size:1
}
.testa {
     font-size:2
}
.testb {
     font-size:3
}
.testc {
     font-size:4
}
.testd {
     font-size:5
}
     
</STYLE>


</HEAD>
<BODY>

<input type="button" value="test" onclick="styletest()">

<P>&nbsp;</P>

<script language="vbscript">

Function styletest()
     alert(document.styleSheets(0).rules.length)
     colRules = document.styleSheets(0).rules
     
     For i =0 To document.styleSheets(0).rules.length -1
          alert(document.styleSheets(0).rules.item(i).style.fontSize)
     Next
End Function
</script>

</BODY>
</HTML>

I tried and searched on trying to get the class names and such, but I could not. So this is what I did. Since I can easily get the value of the style (ie. the fontSize) I thought hey why not add a custom style. So I changed the code to this:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<STYLE id=test1>
.test {
     font-size:1;
     name:test
}
.testa {
     font-size:2;
     name:testa
}
.testb {
     font-size:3;
     name:testb
}
.testc {
     font-size:4;
     name:testc
}
.testd {
     font-size:5;
     name:testd
}
     
</STYLE>


</HEAD>
<BODY>

<input type="button" value="test" onclick="styletest()">

<P>&nbsp;</P>

<script language="vbscript">

Function styletest()
     alert(document.styleSheets(0).rules.length)
     colRules = document.styleSheets(0).rules
     
     For i =0 To document.styleSheets(0).rules.length -1
          alert(document.styleSheets(0).rules.item(i).style.name)
     Next
End Function
</script>

</BODY>
</HTML>

In each rule or class I added a name: style. To the browser this style will do nothing. However now I can loop through and get the classnames so to speak and thus do stuff via that way. Hopefully this helps. Tested in IE 5 btw.
0
 
turbosigAuthor Commented:
Thanks, I guess this is as good as it will get.

Turbo
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now