Forum Moderators: open
First, you need to load all of your stylesheets into the page at pageload (pick one to be the default, and set the others as alternates). When the page loads, the browser will assign a numeric stylesheet ID to each in the order that they were included. To change the stylesheet, just write a loop to set document.styleSheets[stylesheetid].disabled = false or true. If your third stylesheet were green and you wanted to switch to it, for example, you would want to set stylesheetid 1 and 2 to true, and 3 to false. This would enable the green stylesheet and disable the others.
The rest is just a matter of cookies. When someone clicks to change the stylesheet, write a cookie to record the id of the selected CSS. After that, just run a body.onload event to check the cookie and enable the matching stylesheet using the method I just described.
I should point out here that you'll want to be very concious of the number and size of your stylesheets since the user will have to download ALL of them, not just the current one. I learned this the hard way when I once used this method to allow the user to pick between fourteen stylesheets (for accessibility purposes) each about 10k in size. Counting the main page and the external JS file, the user ended up with a 160k download just to see the front page...unacceptable by any measure. So if you're going to do this, make SURE that your css files are compact and fast loading.
[edited by: korkus2000 at 11:02 pm (utc) on Feb. 19, 2004]
[edit reason] No code reviews please [/edit]
Is the relationship between "main page" and "other page" --
you click on a link to go from the main page to the other page... clicking "back" takes you back to the main page...
or
the main page is the page holding a frameset, where one or more pages are opened... main page being the parent, and the other page being the child...
or
the main page is a regular page with an iframe, where one or more pages are opened... main page parent, and the other page being the child...
Is it possible to define the style, relative to the parent?
But, you can do something like this:
document.write('<LINK REL=stylesheet HREF="' + cookiedata + '.css" TYPE="text/css">')
Assuming that 'cookiedata' holds the name of the style sheet to load that will work great.