alternate stylesheets, part 4
we now have the pieces necessary to provide several alternate skins for a website, and provide a visitor with the means to choose one and have it apply across your entire website. finally, a few functions to tie all these pieces together into a library.
the init_skin() function must be invoked sometime after the named stylesheets are defined, but preferably before the page content is loaded, so that the appropriate stylesheets are already enabled when the web browser begins drawing the web page. this is most easily accomplished by including our javascript library in the HTML head section after the stylesheets, and calling init_skin() therein.
the function checks for a cookie named ‘skin’, then tries to make that named stylesheet active. if set_actcss() does not return a value, then it failed (the requested stylesheet does not exist, etc.) and all the skins are left disabled. in that case, we should delete the invalid cookie and make the default skin active instead.
function init_skin () {
if (title = get_cookie('skin')) {
if (! set_actcss(title)) {
delete_cookie('skin');
default_skin();
}
}
}
the default_skin() function looks for the preferred named stylesheet, and makes it active. if there is not a preferred named stylesheet, then it simply gets a list of all named stylesheets and makes the first one active.
function default_skin () {
if (title = get_prefcss()) {
set_actcss(title);
} else if (list = list_stylesheets()) {
set_actcss(list[0].title);
}
}
finally, the function to choose a new skin. set_skin() attempts to make the requested skin active. if this succeeds, then it also sets a cookie named ‘skin’ containing the title of the new skin. otherwise, it makes the default skin active instead.
function set_skin (title) {
if (set_actcss(title)) {
set_cookie('skin',title);
} else {
default_skin();
}
}
on to part 5, putting it to use…

