Solved

Stylesheet object (This may be difficult)?

Posted on 2001-08-23
12
677 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Summer note focus on load 19 188
Round to 2 decimal places 2 36
Table header not displaying on Firefox and Chrome 7 118
Select distinct 25 60
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now