Solved

Stylesheet object (This may be difficult)?

Posted on 2001-08-23
12
683 Views
Last Modified: 2012-06-22
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
Comment
Question by:turbosig
12 Comments
 
LVL 5

Expert Comment

by:raizon
ID: 6420498
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
 
LVL 2

Author Comment

by:turbosig
ID: 6420646
<---
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
 
LVL 2

Author Comment

by:turbosig
ID: 6420668
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
LVL 20

Expert Comment

by:Silvers5
ID: 6420805
you want it on the client side or server side? on server side it's not possible without fso..
0
 
LVL 2

Author Comment

by:turbosig
ID: 6421842
Silvers5,

With fso are you not infact just reading the contents and not using Collections?
0
 
LVL 5

Expert Comment

by:raizon
ID: 6421891
Read the contents into a variable and then use the split along with the mid function to put it into an array.
0
 
LVL 2

Author Comment

by:turbosig
ID: 6422008
You know what, I am trying to use fso, but I don't get everyline returned?
0
 
LVL 2

Author Comment

by:turbosig
ID: 6422051
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
 
LVL 5

Expert Comment

by:raizon
ID: 6422093
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
 
LVL 2

Author Comment

by:turbosig
ID: 6422249
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
 
LVL 2

Accepted Solution

by:
Deathwish earned 150 total points
ID: 6422382
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
 
LVL 2

Author Comment

by:turbosig
ID: 6705600
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Update table value with join syntax 2 76
age from date of birth 4 51
Select case on click 3 19
Writing comments on <p></P> or paragraph 2 19
I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question