I found tvanfosson retheme plugin here. I simplified the plugin to suit my need. It works fine. I have a dropdown select list with numerical option values to choose stylesheets. But then I need to set a cookie for each style. The cookie is created, but had trouble to retain the cookie. Here is the modified plugin:
;(function($) {
$.fn.extend({
retheme: function(options,callback) {
options = $.extend( {}, $.Retheme.defaults, options );
this.each(function() {
new $.Retheme(this,options,callback);
});
return this;
}
});
$.Retheme = function( ctl, options, callback ) {
if (options.styleSheet.match(/^\w/)) {
options.styleSheet = '#' + options.styleSheet;
} else {
//add checking based on url
options.styleSheet = $('link[href*=' + options.baseUrl + ']');
}
$(ctl).filter('select').change( function() {
loadTheme( themeSelector(this), options );
if (callback) callback.apply(this);
return false;
});
};
function themeSelector(ctl) {
var $this = $(ctl);
var theme = $this.attr('href');
if (!theme) {
theme = $this.val();
}
return theme;
}
function loadTheme( theme, options ) {
//var cookedHref = options.baseUrl + theme;
//$.cookie('cthemeHref', cookedHref, { expires: 1 });
//var themeHref = $.cookie('cthemeHref');
var themeHref = options.baseUrl + theme;
if (!themeHref.match(/\.css$/)) {
themeHref += '.css';
}
var styleSheet = $(options.styleSheet);
// set a cookie
$.cookie('links', styleSheet, {expires: 1, path: '/'});
var speed = options.speed;
var delay = options.delay;
var overlay = buildOverlay();
overlay.fadeIn( speed, function() {
styleSheet.attr( 'href', themeHref );
// set a cookie
$.cookie('css', themeHref , {expires: 1, path: '/'});
alert($.cookie('css'));
//$.get( themeHref, function() { // don't want to keep loading
setTimeout( function() {
overlay.fadeOut( speed, function() {
dieOverlay();
});
}, delay );
//});
});
};
$.Retheme.defaults = {
baseUrl: '/styles/',
styleSheet: '',
speed: 'slow',
delay: 500
};
})(jQuery);
Then I place the following to grab the cookies. The problem is all the link in href attributes are replaced by a single selected cookie while I want to grab or activate the cookie one at a time for each selected link.
if($.cookie('css')) {
//$('link').attr('href', $.cookie('css')); //kill all other hrefs
$.cookie('links').attr('href', $.cookie('css')); // $.cookie('links') is not a function
} else {
$(function() {
$('#edit-apple').retheme({baseUrl: '/apple/style-'});
$('#edit-orange').retheme({baseUrl: '/orange/style-'});
$('#edit-mango').retheme({baseUrl: '/mango/style-'});
}
Notes: in apple folder I have stylesheets style-1.css, style-2.css, etc. With else the retheme fails. Without else, it works but no cookie grabbed.
As always any help would be very much appreciated. Thanks