jQuery UI Themeswitcher Cookie Expires

So I disagree with the current setup that is the default configuration with jQuery UI Themeswitcher, it expires the cookie at the end of the session. I mean really if the user is going to change the cookie to some color scheme they like chances are its going to be for longer than the current session and for crying out loud the user is going to expect your site to load the theme they selected last – else it looks broken. Just a poor decision in my opinion – so here is how I fixed it to last 1 year.

Download the themeswitcher.js and save it locally wherever you like.

Now add the “cookieName” below this to the options defition in the file;

    var options = jQuery.extend({
        loadTheme: null,
        initialText: 'Switch Theme',
        width: 150,
        height: 200,
        buttonPreText: 'Theme: ',
        closeOnSelect: true,
        buttonHeight: 14,
        cookieName: 'jquery-ui-theme',
        cookieExpires: 365,
        onOpen: function () { },
        onClose: function () { },
        onSelect: function () { }
    }, settings);

Now modify where the cookie gets made to use this new “cookieExpires” option (line 49);

        $.cookie(options.cookieName, themeName, { expires: options.cookieExpires });

Know you can either use the default set here (365) or set your own in the initializer in your calling script;

    $('#switcher').themeswitcher({ cookieExpires: 365, path: '/', loadTheme: "Sunny"});

Enjoy!

2 thoughts on “jQuery UI Themeswitcher Cookie Expires”

  1. Dude,
    I spent months perfecting my own flavor of the themeswitcher. And sure enough I started allowing my users to theme certain things. And during my testing I noticed the cookies were set to default (expire on browser session termination.) L-A-M-E.

    So not knowing cookie code by heart I googled and found your page. Thanks for making my life that much easier. YOU ROCK!

Leave a Reply