|
CSS Switcher/Stylesheet Chooser
Here is an example of a CSS Switcher in ASP.
http://computer-helpforum.com/asp/a...SS_Switcher.asp
Check to see if the user requested a new CSS file
asp Code:
Original
- asp Code |
|
|
|
'Check to see if cssSelector was submitted If Request("cssSelector") <> "" Then 'Save the cookie with the style selection Response.Cookies("cssSelected") = Request("cssSelector") Response.Cookies("cssSelected").Expires = DateAdd("y",1,Now) 'Optional cookie attributes 'path_info = Request.ServerVariables("PATH_INFO") 'divider = InStrRev(Request.ServerVariables("PATH_INFO"),"/") 'path = Left(path_info,divider) 'Response.Cookies("cssSelected").Domain = Request.ServerVariables("SERVER_NAME") 'Response.Cookies("cssSelected").Path = path 'Redirect to refresh the page Response.Redirect(Request.ServerVariables("SCRIPT_NAME")) End If
The CSS files can be loaded from the server
asp Code:
Original
- asp Code |
|
|
|
'Get CSS files using FSO. If you want to load a list of .css files from the server then set getCSSFiles = True getCSSFiles = False If getCSSFiles Then set fs=Server.CreateObject("Scripting.FileSystemObject") set fo=fs.GetFolder(Server.MapPath(".")) for each FileName in fo.files If Right(FileName.Name,4) = ".css" Then cName = FileName.Name cTitle = UCase(Left(FileName.Name,1)) & LCase(Mid(FileName.Name,2,Len(FileName.Name)-5)) Redim Preserve cssArr(1,i) cssArr(0,i) = cName cssArr(1,i) = cTitle i=i+1 End If next set fo=nothing set fs=nothing
or hardcoded
asp Code:
Original
- asp Code |
|
|
|
Else 'Hardcoded CSS files. getCSSFiles = False ReDim cssArr(1,2) cssArr(0,0) = "red.css" 'Name of CSS file cssArr(1,0) = "Red" 'Title/Description of CSS file cssArr(0,1) = "green.css" cssArr(1,1) = "Green" cssArr(0,2) = "blue.css" cssArr(1,2) = "Blue (serif)" End If
Grab the selected CSS from the cookie or set a default
asp Code:
Original
- asp Code |
|
|
|
'See if a cookie is present If Request.Cookies("cssSelected") <> "" Then 'If it is then set the CSS variable to the cookie value. css = Request.Cookies("cssSelected") Else 'Otherwise default it to something else. Here it is the first value in the array (red.css) css = cssArr(0,0) End If
Set the CSS
Code:
<link href="<%=css%>" rel="stylesheet" type="text/css">
Output the CSS choices
Select/Dropdown
asp Code:
Original
- asp Code |
|
|
|
<form method="post" action=""> <select name="cssSelector"> <%For x=0 To Ubound(cssArr,2)%> <option value="<%=cssArr(0,x)%>"<%If cssArr(0,x) = css Then%> selected<%End If%>><%=cssArr(1,x)%></option> <%Next%> </select> <input name="setStyle" type="submit" id="setStyle" value="Set Style"> </form>
or Links
asp Code:
Original
- asp Code |
|
|
|
<%For x=0 To Ubound(cssArr,2)%> <a href="<%=Request.ServerVariables("SCRIPT_NAME")%>?cssSelector=<%=cssArr(0,x)%>"><%=cssArr(1,x)%></a><br> <%Next%>
|