Moveable Style II

Being unable to leave well enough alone, I’ve added another stylesheet (Salmon Roe) and an improved version of styleswitcher.js which also handles the Netscape 4 issue.

The first change in mystyleswitcher.js is setting the cookie at the time a new stylesheet is chosen, rather than when leaving the page as the original script did. This fixes the odd instances where you switched styles, then opened a comment window which then appeared in the previous style. I thought this was important for checking out stylesheets and also cutting down on cookie (re)setting in general, though if the cookie gets corrupted the user will get no style at all. (Thanks to Jade for discovering the bug.) That situation can always be rectified by selecting a new stylesheet manually.

The Netscape 4 handling is just something that seems to work. I changed the javascript function that was used to look up the first stylesheet (that was, the one with a rel=”stylesheet” instead of a rel=”stylesheet”) so
that it would look up a stylesheet with “Default” in its title instead. [I’m not doing it this way anymore - I just hardcoded the name of my default stylesheet to save computing time. To see the code for finding a stylesheet with “Default” in the title, check out the broken switcher. (That wasn’t the broken part.) To see the original code for finding the primary stylesheet, see the original switcher. The function is called getPreferredStyleSheet() in both places.]

In effect,
this gives two default stylesheets, one for browsers that can’t do the javascript switching at all (that is, the rel=”stylesheet” one), and another for browsers that can (with title=”Default …”). I already had a default stylesheet for this blog (Classic Khaki, whose real title is Default Khaki) as well as a version of it I’d dumbed down for Netscape 4 (Netscape 4 Khaki, a.k.a. For Older Browsers), so I
just put those in the appropriate spots.

Next time, I’ll make that Ume Shiso Maki stylesheet, and perhaps spruce up Lavender’s Blue.

5 Responses to “Moveable Style II”

  1. Caff Says:

    Let me see if I get this straight: basically, the JavaScript function determines what browser I use and picks the shyle sheet accordingly? (7:50 am here, and I just got up. Bear with me, please.)

    Also, how does a cookie get corrupted? Because that seems to be the case with me. Every time I load your blog page, I get no style sheet at all; I have to pick one manually. But even when I do that, the comment window still pops up without style sheet.

  2. Jemima Says:

    Well, corrupted wasn’t the best word for it. It would be difficult for a browser to corrupt a cookie, though not beyond the talents of, say, Netscape 4. It’s more likely that the cookie could become outmoded when the stylesheet corresponding to that name is either removed or renamed by the blogger. (I renamed one or two of mine in the course of experimenting.)

    The javascript essentially runs through all stylesheet LINKs, turning on the one that has the title named in your cookie and turning off the rest. If you have no cookie at all, the “Default” is used. If you have a bad cookie, all stylesheets are turned off and you get the unstyled page.

    The “Default” for the javascript doesn’t have to be the primary stylesheet - that was my change for Netscape 4. Netscape 4 doesn’t do the turning on and off thing at all, so it just uses the primary stylesheet whatever you choose, regardless of the cookie. (Or so it seems so far.) So the javascript isn’t detecting the browser actively; it just fails to turn off the primary stylesheet and turn on another one if the browser is incompetent. It’s passive browser detection, I suppose.

    I also introduced a little bug in the javascript that didn’t give my Mac Mozillas any problems but probably caused your white page. I think it’s fixed now, though it’s hard for me to tell from home. I’ll track it down tomorrow if it’s still there.

  3. Jerie Says:

    I’m getting a white page, too, using IE 6. I could see all the stylesheets yesterday, but cleaned my cookies last night, so I’m not sure if that had anything to do with it or not.

    I like what you can do with MT. Thanks for sharing.

  4. Jemima Says:

    IE is psychotic. I’ve juggled some of my improvements around so that it really should be fixed now, unless I break it again when I put the cookie bits back in. Sigh.

  5. Jerie Says:

    Looks good now. Very cool. And the comments box matches the stylesheet, too. Very, very cool.