Stylesheet Switcher - Part 2

Posted on Friday, August 18th, 2006

Yesterday I began to create a stylesheet switcher using javascript and the HTML DOM. I finished up with an example of the work so far, but I failed to address a few of the points in my requirements, namely:

  • Use a dropdown box to select styles
  • Populate the dropdown box with stylesheet names from the title attribute of the link tag
  • Graceful degradation in browsers without script
  • Future proof javascript

Today I will tackle these issues.

Dropdown Boxes

This isn’t really a javascript issue, but not having had much experience with dropdown boxes, I had to look this up. A dropdown box is an element of an HTML form, so must exist within the form element. However, since we aren’t using server side scripts there is no need to give the form a method or an action. In fact, the dropdown box will trigger the script itself. We set up the dropdown box as so:

<form>
<select onChange="setStyleSheet »
(this.options[this.selectedIndex].text)”>
<option>stylesheet</option>

So we use the function created yesterday to change the stylesheet, but what is this.options[this.selectedIndex].text? I’ll break it down:

this.options
this refers to the dropdown box and options will be the <option> elements that will populate the list
this.selectedIndex
selectedIndex returns the position of the option selected (starting with 0)
[this.selectedIndex].text
appending .text to the selectedIndex means that it will return the text within the option element

In all, when an option is selected, the option text that was selected will be handed to the setStyleSheet function. The only drawback of this method is that the text in each of the options has to match the titles of your stylesheets in order that the function works. This is where javascript steps in again.

Self Populating Options

Rather than creating the dropdown box in the HTML source of my site, I decided to let javascript do it. This has two bonuses, firstly a user who has disabled javascript won’t see the dropdown box and won’t know what they are missing out on (graceful degradation - we don’t want a dropdown box on the page if the user won’t be able to use the functions) and secondly we can use our functions we came up with yesterday to iterate through the available stylesheets creating an option for each one.

“This will be easy,” I thought. So I started writing a whole bunch of document.write(…) statements ready to output the dropdown box. However, there have been changes in what is and isn’t allowed within javascript, chages I came across whilst reading about good javascript practices. I was surprised to see that document.write will not work with XHTML. The reasons were compelling enough and, even though I use HTML 4.01 throughout this site, having a script that will work for HTML and XHTML is important for the future.

The DOM and createElement

So I needed a new way of dynamically producing a dropdown box. Rather than writing out the HTML with document.write, it turns out that modern javascript programmers create all the objects separately and then add them to the DOM tree. There is a superb DOM tutorial at webreference.com, and it taught me everything I needed to know to create my dropdown box. I’ll walk through the method then post the code so that you can see how it all happened.

  1. Create the form element
  2. Create the select element
  3. Add the onChange attribute to the select element
  4. Append the select element to the form
  5. Get the stylesheets
  6. Get the stylesheet that is currently in use (the preferred stylesheet)
  7. For all the stylesheets:
    1. Get the title of the stylesheet
    2. Create an option element
    3. If the stylesheet is the preferred stylesheet, add the selected attribute to the option element
    4. Create a text node using the title of the stylesheet
    5. Append the text to the option element
    6. Append the option to the select element
  8. Append the form to the area of the document that you select

The last point needs some extra explanation. Due to the nature of creating and appending elements to the DOM tree, rather than writing HTML directly to the page as it is processed, the form element needs to be appended to a relevant element that already exists in the DOM tree. To deal with this I included a parameter in the call of the function. All you need to do is add the id of the element you want the dropdown box to appear in. The code will show how it works.

function linkBox(elementID) {
var form = document.createElement("form");
var drop = document.createElement("select");
drop.setAttribute("onChange",»
“setStyleSheet(this.options[this.selectedIndex].text)”);
form.appendChild(drop);
var stylesheets = getStyleSheets();
var preferred = getPreferredStyleSheet();
for (var i=0;i<stylesheets.length; i++) {
var title = stylesheets[i].getAttribute(”title”);
var option = document.createElement(”option”);
if (title==preferred) { »
option.setAttribute(”selected”,”selected”); }
var optionText = document.createTextNode(title);
option.appendChild(optionText);
drop.appendChild(option);
}
document.getElementById(elementID).appendChild(form);
}

The Final Product

Here is the final demonstration of the stylesheet switcher with dropdown box. You can download the full script here.

To use the script, just add your stylesheets and the script to the header as yesterday then place the following script where you want your dropdown box to appear:

<script type="text/javascript" language="javascript">
<!--
linkBox(’elementID’);
//-->
</script>

I hope this will be useful, for both accessibility and adding new styles to a site. If you do use it and would like to show off (or have problems with it), please let me know. Also, if you have any ideas for accessibility driven bits of code that either exist and need updating/explaining, or should exist, tell me and I will see what I can do.

Problems! Turned out that this doesn’t work in Internet Explorer (oh don’t we love it so). You can, however, find out how I fixed it.

If you enjoyed this post, why not subscribe to Unintentionally Blank

Comments

  1. Pingback by The DOM again - A Style Sheet Switcher :: Unintentionally Blank

  2. Pingback by Stylesheet Switcher - Part 3: Revenge Of IE :: Unintentionally Blank

  3. Nice Work! Says:

    Nice Work!!! There is no problem loading scripts in divs the issue is M$ ie limits 30 style sheets anything beyond gets ignored.


  4. Mike Says:

    Thanks for your ideas!


  5. Phil Says:

    Hi Mike, thanks for dropping by. Do you have somewhere you were going to use the switcher?


  6. Pingback by Ways to build Style Sheet Switcher for your own web site at Witty Sparks

  7. jay Says:

    the code worked fine in Firefox, however when I tried using Explorer, it didn’t seem to work.


  8. Phil Says:

    Hi Jay,
    You’re right, it doesn’t work in Internet Explorer. I fixed that in the final version though.


  9. Peter Says:

    Thanks for your tutorial. I have a question, I have broken my css into several files, 3 for different css skins, 1 for default styling, and 1 for reset. It seems your solution is not suitable for such a setup. Is there a way around it besides lumping the same codes in each stylesheet? Much appreciated.


  10. Phil Says:

    Hi Peter,

    You are right, at the moment there is no way to split up your CSS files into a more modular manner and always include your base styles using normal includes of CSS files.

    You could create say, default.css and reset.css as well as style1.css and style2.css, then have two further CSS files that use @import command to import the 3 sheets that you need like this:

    @import url(reset.css)
    @import url(default.css)
    @import url(style1.css)

    and a similar one for style2.css. These two style sheets could then be included and used with my style sheet switcher.

    Other than that, I need to rewrite the code for it anyway, to improve several facets of the code. When I get round to this, I will include an option to declare default style sheets that always get loaded.


  11. Pingback by bdITjobs.com : : Blog » Blog Archive » Ways to build Style Sheet Switcher for your own web site

  12. Andrei Floris Says:

    Have you finished your work for stylesheet switcher? DO you want to sell it after, relies it, or just post it for free download on internet! The examples you presented look great!


  13. Go-Gulf Says:

    Nice Style Sheet Switcher. I have check the code on Firefox, Internet Explorer, Maxthon and it works fine.
    How can i integrate it with XHTML ?


  14. replica wallet Says:

    the encipher functioned finely incoming Firefox, nonetheless while I examined habituating Explorer, it didn’t appear to exercise.


Leave a Comment