Somewhat easily. For this, we'll need a web page (switch-styles.html) and two style sheets (style1.css, style2.css). I would also suggest storing the javascript in a separate file, but that's another FAQ.
ok, first things first: the style sheets.
style1.css
CODE
/* default style sheet for style-switcher.html */
body { color: blue; font-family: monospace; font-weight: bold; }
next, we'll need javascript. hopefully the commenting i've provided is good enough so you can understand what's going on.
CODE
<script type="text/javascript"><!--
// our two style sheet paths var cStyleSheetBlue = "style1.css"; var cStyleSheetRed = "style2.css";
// this function changes the style sheet when the user clicks on a link // and then calls the storing function function changeStyleSheet ( strLinkId, strStyleSheetLocation ) { // switch the stylesheet location document.getElementById( strLinkId ).href = strStyleSheetLocation; // call the storing function storeStyleSheetInCookie( strLinkId, strStyleSheetLocation ); }
// this function stores the style sheet preference in a cookie on the user's machine // the cookie expires one year from the time it is stored // and can be read from any page on your website function storeStyleSheetInCookie ( strCookieName, strStyleSheetLocation ) { // get today's date var dtExpire = new Date(); // add a year to the date dtExpire.setYear( dtExpire.getFullYear() + 1 ); // store the cookie document.cookie = escape( strCookieName ) + "=" + escape( strStyleSheetLocation ) + "; expires=" + dtExpire.toGMTString() + "; path=/;"; }
// this function will attempt to retrieve the preffered stylesheet url from // a cookie on the user's machine function getStyleSheetFromCookie ( strLinkId ) { // the name of the cookie we are trying to find strLinkId = escape( strLinkId ); // if there are no cookies, leave the function if ( document.cookie == "" ) { return false; } else { // store the raw cookie data in a string var strCookie = document.cookie; // look for the cookie's name var intFirstChar = strCookie.indexOf( strLinkId ); // if the cookie we want does not exist, leave the function if ( intFirstChar < 0 ) return false; // look for the end of the cookie value var intLastChar = strCookie.indexOf( ";" ); // if there is no separator, we'll just take the whole thing if ( intLastChar < 0 ) intLastChar = strCookie.length; // skip the name= part of the cookie (so we only get the data we want) intFirstChar = strLinkId.length + 1; // return the style sheet's url return unescape( strCookie.substring( intFirstChar, intLastChar ) ); } }
// this function will set the preferred style sheet (if one exists) when the // page initially loads function preloadStyleSheet ( strLinkId ) { var strPreferredStyleSheet = getStyleSheetFromCookie( strLinkId ); if ( strPreferredStyleSheet != false ) changeStyleSheet( strLinkId, strPreferredStyleSheet ); }
// attach the preload function to the onload event onload = function() { preloadStyleSheet("ss") };
//--></script>
and last, but of course not least, we'll need the html.
switch-styles.html
CODE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">