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. Monica Newton Says:

    When dealing with alternate stylesheets, is it possible to include only the pieces I want to change (e.g. fonts), or do I have to reproduce the entire stylesheet again? I’ve found a couple of really good resources (stackoverflow.com, exceptionalsheets.com) but can’t seem to locate a definitive answer anywhere. Thanks in advance for your help.



cheap online viagra
viagra 25 mg order
cialis 10mg
100 mg cialis us pharmacy
viagra and cialas
generic levitra wholesale 100mg
viagra by phone
buy viagra online 50mg
buy cheap p viagra
cheap viagra online order viagra now
cheap viagra online uk
buy real viagra pharmacy online
cheapest price for viagra
cheapest generic substitute viagra
buy cheap online prescription viagra
buy cheapest viagra
rx cialis 100mg
canada pharmacy discounted viagra 100
generic cialis wholesale 100mg
cialis 50 mg
cheap viagra uks
discount drugs cialis 100mg
buy viagra on the internet
viagra by mail canada
100mg levitra
cheapest generic silagra viagra
cheep generic viagra
cheapest place to buy viagra online
buy levitra online viagra
buy generic viagra online
cheap 25mg levitra
viagra 100mg price
buy kamagra viagra india
cheap cialis canada
buy levitra online
cheap viagra canada
cheap viagra st
cialis 50mg online
viagra and cialis and
buy from pharmacy us viagra
viagra brand
viagra buy oonline
viagra brands
buy get online prescription viagra
cheapest viagra
buy levitra viagra
buy levitra viagra online
buy online prescription viagra without
10 generic viagra for 19.95
buy cheapest online place viagra
cialis 10
buy say viagra
cheap phizer viagra
cialis 2.5
buy generic online viagra
buy levitra canada
cialis dosage 20mg
cheap online softtabs viagra
levitra no prescription
50 mg viagra retail price
buy site viagra
viagra buy general
viagra online prescription
order levitra online
rx viagra 100mg
buy cheap cialis
buy cheap viagra online now uk
buy cheap online viagra viagra
cialis 100mg price
buy discount cialis
cheap overnight viagra
20mg cialis
5mg cialis generic
buy cheap viagra on the net
buy real viagra online
viagra by the pill
cheapest generic viagra
buy kamagra viagra
cheap viagra uk
cheap viagra online pharmacy online
cialis 20mg
buy online drug viagra pharmacy
buy 10 mg cialis
buy sale viagra
buy viagra online and get prescription
cialis black reviews
cheapest uk viagra
viagra and cialis cheap
buy online prescription viagra
buy in online usa viagra
buy viagra no prescription
buy cheap viagra in uk
buy in uk viagra
cheapest place to buy viagra
buy keyword viagra
buy cheap viagra online now
generic viagra wholesale 100mg
cheap order prescription viagra
buy levitra wighout prescription
buy cheap cialis generic levitra viagra
cheap online viagra viagra
cialis no prescription
cheap online pill viagra
purchase levitra online
buy prescription viagra without
cialis 10mg
levitra 100mg price
viagra buy viagra online
viagra by overnight delivery
cheapest viagra in the uk
cheapest place buy viagra online
cialis 20 mg
buy pharmaceutical viagra
buy cialis no prescription
buy online pill viagra
buy cheap deal pill viagra
250 mg cialis
buy real viagra online pharmacy
viagra buy generic
cheap levitra online
cheap site viagra
buy free viagra viagra
buy viagra online
buy cialis online 50mg
viagra buying
cialis 10 mg
buy viagra wighout prescription
viagra 25 mg
cheapest prescription viagra
buy cialis 50mg
viagra 50mg
40 grams of cialis
levitra 10mg 20mg
cialis 24
buy levitra no prescription
buy cheap viagra online u
cheap price viagra
buy discount viagra
buy pharmacy pill viagra
viagra without prescription
cheap 25mg cialis
cheap discount levitra
viagra brazil
buy cheap viagra on
levitra 50mg
cheap online price price viagra
buy viagra online at cheap price
viagra buy australia
buy generic viagra
cheap order site viagra
buy now levitra
cialis 25 mg order
levitra 50mg online
buy cialis online
cialis reviews
buy cialis wighout prescription
cheapest generic viagra and canada
buy cheap viagra online
cialis 30
buy line viagra
buy generic viagra online pharmacy online
levitra 25 mg order
cheap online order viagra
cheapest uk supplier viagra
250 mg viagra
viagra buy viagra
buy online sale viagra viagra
cialis 25 mg
cheapest generic price viagra
cialis 2005
viagra 50mg online
buy in online uk viagra
cheap levitra online
viagra by money order
buy cheap viagra prescription online
buy cialis online now
cheapest generic viagra and cialis pills
cheapest prices for viagra online
50mg cialis
cialis 2005
viagra by mail order
buy cialis internet
cialis 5mg
buy cheap levitra
buy cheap sale viagra
viagra and coupon
levitra 20 mg
viagra 10mg 20mg
cialis 10mg 20mg
buy cheap discount levitra
buy now online viagra
usa cialis
buy online price viagra
buy online levitra cialis viagra
100 mg viagra us pharmacy
viagra buy now pay later
buy cialis no prescription
cheapest price for generic viagra
cheapest regalis viagra
viagra 100mg
cheap levitra canada
buy levitra 50mg
cialis 50mg
purchase levitra
cialis 20
national bingo night
cheap online sales viagra
buy online purchase viagra
viagra buy ionline
cheap viagra online
cheap pill pill sale viagra
buy cialis online viagra
cheapest online viagra
buy free viagra on internet
50 mg cialis retail price
levitra online
order levitra
cheap soft tab viagra
viagra buy uk
cialis 10 mg
buy pfizer viagra
cialis black 800mg
buy prescription viagra
buy viagra on-line
generic cialis
cheap online generic viagra
buy online online viagra viagra
buy p viagra
10mg cialis
cialis 20mg
buy viagra online a href
viagra buying online
20 mg cialis
cheap prescription viagra without
cialis without prescription
order 50mg cialis
cheap pharmacy viagra
buy cheapest online viagra
buy cheap cialis
buy generic viagra usa
cheap viagra without prescription
cheap viagra online prescription
allegra
viagra by mail catalog
voagra online without prescription
buy online uk viagra
buy online online pill viagra viagra
cheapest in uk viagra
canada pharmacy discounted levitra 100
free bingo
50 mg viagra
buy cheap viagra
36 hour levitra
tamiflu
buy viagra online alternative viagra
buy generic viagra img
buy online sale viagra
cialis 20
generic viagra 20mg pills erections
50 mg levitra
viagra buy do nu
viagra buy it online now
cialis 30mg
buy now viagra
cheep viagra
levitra 50 mg
order 50mg viagra
buy discount levitra
buy online pharmacy viagra
cheapest line viagra
cheap 25mg viagra
buy line viagra where
cialis 32
buy 100 mg viagra
10 generic cialis for 19.95
buy viagra onli
buy cheap viagra online uk
buy discount levitra
cheapest price on viagra
cheap cialis canada
levitra brand levitra 100mg
viagra 50 mg
buy generic no online prescription viagra
cheap online purchase viagra
buy deal deal price viagra
buy price viagra
buy cheapest viagra online
buy generic viagra buy
cialis without prescription
buy viagra 50mg
buy cheap viagra uk
buy prescription vaniqa viagra
levitra 25 mg
viagra buy
cheapest prices on generic viagra
cialis 5m tablets
buy cheap online uk viagra
buy cheap generic viagra
voagra online without prescription
buy pill price price viagra
buy discount propecia
cialis 100mg
viagra by mail
cialis 36 hours
buy cheap online viagra
cheap pharmaceutical viagra
cialis 50mg
buy later now pay viagra
buy cheap uk viagra
buy cheap viagra cheap viagra online
cialis online
buy cheap generic online viagra
viagra no prescription
buy locally viagra
buy file viagra
generic levitra 100mg
cialis 10
buy free online sale viagra viagra
cheapest price viagra
order 50mg levitra
cialis brand cialis 100mg
buy real viagra
cheapest generic viagra and cialis
generic cialis 20mg pills erections
cheapest cialis
rx levitra 100mg
10mg viagra
buy cheap viagra
cheapest site viagra
buy cheap site viagra
cialis 20 mg
levitra 100mg
viagra buy in uk online
generic levitra 20mg pills erections
discount drugs levitra 100mg
cheap cialis canada
buy online order viagra
discount drugs viagra 100mg
cialis 1
viagra 125 mg
online slots
cheap cialis canada
buy levitra now
buy generic viagra pharmacy online
36 hour viagra
cheapest cheap viagra
cialis online prescription
100mg viagra
voagra online without prescription
100mg cialis
buy cheap deal viagra viagra viagra
buy low price viagra
buy in spain viagra
levitra online prescription
canada pharmacy discounted cialis 100
buy pill viagra
250 mg levitra
cialis 20mg reviews
buy cialis now
buy generic viagra viagra
cialis 125 mg
levitra without prescription