<?xml version="1.0" encoding="utf-8"?>
<tutorial>

<description>Learn how to allow users to switch between style sheets using Javascript</description>
<keywords>tutorial, switch, style, sheets, stylesheets, javascript, switcher, change, alternate, multiple, changing, paul sowden</keywords>

<title>Style Sheet Switching</title>
<slug>By using a little Javascript, you can allow users to switch between different style sheets and dramatically alter the appearance of a web page's layout. (Yeah, we thought the idea was pretty cool on Spoono, too. Hence, why Spoono v4 has 7 different style sheets to choose from).</slug>

<text>

<p>Please note that while this is an excellent way to switch styles, it is not used in Spoono v4's flavor switching. For serveral reasons explained later in this tutoral, we chose to use PHP cookies to control the style sheets instead. The highly compatible Javascript method explained here was originally created by <link><url>http://idontsmoke.co.uk/</url>Paul Sowden</link>. To start things off, let's take a look at some code for loading CSS files:</p>

<p><green><![CDATA[&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;persistent.css&quot; /&gt;]]></green><br /><br />
This first method creates a <i>persistent</i> style sheet. This means that the style sheet will definitely be loaded and enabled for use on this web page. Most style sheets used on the web are loaded in this manner by placing this code in the <green>head</green> area of an HTML document. The properties of the style change when we add a title attribute, like so:</p>

<p><green><![CDATA[&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;preferred.css&quot; title=&quot;swanky&quot; /&gt;]]></green><br /><br />This tag loads a css file, just like before, except this time, the file is given a title and thus it becomes known as a <i>preferred</i> style sheet. By default, this style sheet will be loaded and enabled for use on the web page. The difference with preferred style sheets is that they can be disabled (turned off) when other <i>alternate</i> style sheets are turned on. Multiple style sheets can be given the same title and they will be turned on and off as a group. So, the next thing we need to know is how to define an <i>alternate</i> style sheet. The code looks like this:</p>

<p><green><![CDATA[&lt;link rel=&quot;alternate stylesheet&quot; type=&quot;text/css&quot; href=&quot;alternate.css&quot; title=&quot;groovy&quot; /&gt;]]></green><br /><br />
As you can see, the <green>rel</green> attribute is changed to alternate stylesheet. By default, alternate style sheets are loaded by the browser, however they are <i>disabled</i> (turned off). Multiple alternate style sheets can be given the same title as before, and they will function as a group, just like above. The fact that alternate style sheets are fully loaded by the browser is the main reason that we avoided using them in Spoono v4. Each of our flavor style sheets loads many images, and by using PHP cookies to control our style sheets, only the currently active flavor's style sheet and images are loaded (as a persistent style sheet). If you have a small number of style sheets or few images in them, the Javascript technique which we are learning is a great way to go. Getting back on track, the next thing we need to do is devise a way to switch between preferred and alternate style sheets. If all browsers functioned like Mozilla and provided a handy menu for switching between styles (a button in the lower left corner), we might not even have to use Javascript to do this. However, most Mozilla users probably don't even know the menu exists, and the vast majority of web surfers which use Internet Explorer are plain out of luck. So, it becomes crucial for the style savvy web designer to implement style switching controls in the web page layout.</p>

<p>Numerous Javascript functions will be needed to scan through all the <green>link</green> tags, enable/disable style sheets, identify which style is active, and store the currently enabled style preference in a cookie, so it will remain enabled as the user browses other web pages. Because writing cookies is outside the scope of this tutorial, it may be best to accept that a Javascript include file written by <link><url>http://www.quirksmode.org/</url>Peter-Paul Koch</link> and <link><url>http://idontsmoke.co.uk/</url>Sowden</link> does the job wonderfully. Thus to keep things simple, all you need to do is download <link><url>/javascript/tutorials/styleswitching/styleswitcher.js</url>styleswitcher.js</link> and include it in your web document with the following code (usually placed in the <green>head</green> area as well):<br /><br />
<green><![CDATA[&lt;script type=&quot;text/javascript&quot; 
src=&quot;styleswitcher.js&quot;&gt;&lt;/script&gt;]]></green></p>

<p>The final step is to create links on a web page to control the styles. To do this, you'll need to apply an <green>onclick</green> attribute that passes values to the functions defined in the styleswitcher.js. It's quite simple. Here's the code to create two links that will switch between styles with the titles &quot;swanky&quot; and &quot;groovy&quot; which were defined in the example code above:<br /><br />

<green>
<![CDATA[&lt;a href=&quot;#&quot; 
onclick=&quot;setActiveStyleSheet('swanky'); 
return false;&quot;&gt;change to swanky&lt;/a&gt;]]>
<br /><br />
<![CDATA[&lt;a href=&quot;#&quot; 
onclick=&quot;setActiveStyleSheet('groovy'); 
return false;&quot;&gt;change style to groovy&lt;/a&gt;]]>
</green>
</p>

<p>You may view this <link><url>example.htm</url>working example</link> to make sure you have it all set up properly.</p>

<p>Now you have everything necessary to make your own Javascript-powered, style switching web page. Oh, how the neighbors will be jealous.</p>

</text>
</tutorial>