Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read a cookie value into CSS? 1

Status
Not open for further replies.

markdmac

MIS
Dec 20, 2003
12,340
US
Hi guys. I'm looking to do much the same as was in this thread thread215-1101027.

I've written data to a cookie. It contains background color, font color and font size info that the user has selected on a customization page.

I'd like to be able to create a CSS file to read the data in the cookie if it is there and use those values, otherwise use my default values.

The examples given in the listed thread don't help me unfortunatley. I'm using IIS, ASP.Net and VBScript.

Can anyone provide a sample CSS file that can read a cookie and change values in the CSS based on the cookie data? Can this even be accomplished? I would like ot avoid having to set up a database to store the user preferences on the server.

Thanks in advance.

Mark
 
Thanks cLFlaVA but as specified I'm looking to stick with VBScript.

I don't know if what I want to do can even be accomplished, but I don't want to have a bunch of alternate CSS files on the server. I'm giving my users about 150 different colors to choose from. So I would end up with hundreds (perhaps thousands) of CSS files if I went that route to allow the many permeantations of color, font color, font size, font bold.

Maybe a better question for me to ask is, is there a way to dynamically build a CSS file based on the data I read from a cookie? Or can a CSS file even include VBScript code? I don't know the limitations as I am new to CSS.

I hope you find this post helpful.

Regards,

Mark
 
You might be able to dynamically change the css file with some sort of server side process, but let's think about it for a second.

The 2 major things (note - not everything, just the 2 major things) that come to my mind when putting all your css code in a seperate file to be included is:

1. makes it easier to globally change the layout and appearance of your site by only having to edit 1 file.

2. the css file can be cached for the browser for faster page loading.

Now.... if you're going to have a server side process that can dynamically change the code in the css file, the browser can't reliably cache it because it could change with each page load, so #2 is not an issue in your case.

So, that means the main reason that you'd want to put your css in a seperate file is for ease of editing across your site. Since importing css files is not the only way to do this, why not take another route?

You could instead use Server Side Includes to pull in all of your css declarations and embed them inside of <style> tags directly in the page. Since you can include asp (or asp.net) code inside of your server side include files (since they are imported before asp compiles the code), this would allow you to change the color values for all of your css.

So your server side include would look like:
Code:
<style type="text/css">
<%
   bgcolorvalue = "#abcdef"
%>
body {
   background-color:[!]<%=bgcolorvalue%>[/!]
   etc.....
}
</style>

and you could pull it into your page like so:
Code:
<head>
<title>blahblah</title>
[!]<!--#include File="mycssfile.asp"-->[/!]
</head>

anyway.... just an idea

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Now we really are getting somewhere.

<%
bgcolorvalue = "#abcdef"
%>
Do you think this will work with code to check for the cookie?

In other words:
Code:
If Not Request.Cookies("Preferences") Is Nothing Then
            docBackColor = _
                 Server.HtmlEncode(Request.Cookies("Preferences")("MyBgColor"))
            
End IF

I hope you find this post helpful.

Regards,

Mark
 
Do you think this will work with code to check for the cookie?

Absolutely, as long as all the cookie code is done server side. ASP supports cookies so you shouldn't have a problem pulling the value from there.

The only big obstacle you're going to have to overcome is redoing all your pages to pull the css in from the SSI instead of the @import or <link> calls. The rest should be a piece of cake.

On a side note, there's lots of documentation on server side cookies using ASP. I use server side JScript to code all my ASP so I can't offer much along the lines of a solution for VBScript (the main reason I stay away from the ASP forum [lol]) - only something that I could find googling, and you don't really need me to do that for you I'm sure [wink]

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Kaht, a star for you. This worked great THANKS!!!!

This will save me TONS of work now. Much appreciated.

I hope you find this post helpful.

Regards,

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top