Follow these step to create custom layout according to user cookie:-
Create a function which read the cookie and change the default.css
function readCookieCSS(){
if($.cookie('styleSheet') === null || $.cookie('styleSheet')==undefined) {
/*Do nothing*/
}
else{
$('link[href="default.css"]').attr('href',$.cookie('styleSheet'));
}
}
Changing the stylesheet and saving the css file name in cookie.
function saveCSSinCookie(CSS_FILE_NAME){
if($.cookie('styleSheet') === null || $.cookie('styleSheet')==undefined) {
$.cookie("styleSheet", CSS_FILE_NAME, { expires : 365 });//expires after 1 year
$('link[href="default.css"]').attr('href',$.cookie('styleSheet'));
}
else{
$('link[href="'+$.cookie("styleSheet")+'"]').attr('href', CSS_FILE_NAME);
$.cookie('styleSheet',CSS_FILE_NAME);
}
}
The basic idea behind these functions is if your cookie exist then stylesheet is saved by the user else the stylesheet is default.css (which in your case can be any one of the above styleSheet).
So, You have to call the readCookieCSS() on page load itself and the second function whenever you need to change the style.
Look into these answer if you want to learn more about stylesheet change and Jquery Cookie:-
StyleSheet Change
JQuery Cookie
Calling function in your code:-
Add the readCookieCSS() when the page is load:-
$(document).ready(function(){ readCookieCSS();});
Add the saveCSSinCookie(CSS_FILE_NAME) is called when you change the stylesheet by updating your current bind function like this:-
.bind('change',function(){ saveCSSinCookie($(this).val()); });
Updated Javascript as I am storing the different css value and title in javascript array $links
Code:-
function readCookieCSS() {
if ($.cookie('styleSheet') === null || $.cookie('styleSheet') == undefined) { /*Do nothing*/
} else {
$('link[href="default.css"]').attr('href', $.cookie('styleSheet'));
}
}
function saveCSSinCookie(CSS_FILE_NAME) {
if ($.cookie('styleSheet') === null || $.cookie('styleSheet') == undefined) {
$.cookie("styleSheet", CSS_FILE_NAME, { expires : 365 });//expires after 1 year
$('link[href="default.css"]').attr('href', $.cookie('styleSheet'));
} else {
$('link[href="' + $.cookie("styleSheet") + '"]').attr('href', CSS_FILE_NAME);
$.cookie('styleSheet', CSS_FILE_NAME);
}
}
$(document).ready(function()
{
readCookieCSS();/*reading cookie on page load*/
var $links = [{'style':'style/black.css','title':'black'}, {'style':'style/blue.css','title':'blue'}, {'style':'style/green.css','title':'green'}, {'style':'style/red.css','title':'red'},{'style':'style/yellow.css','title':'yellow'}];
$('#sw').append('<div id="toolbar"><select id="css-selector"></select></div>');
var options= '<option value="style/default.css">Select color:</option>';
$links.map(function(value,index){
options +='<option value="'+value.style+'">'+value.title+'</option>';
});
$('#css-selector').append(options)
.bind('change',function(){
saveCSSinCookie($(this).val());
});
}
);
Using a default style sheet for the user for first time than changing stylesheet accordingly
Updated CSS:
<link href="default.css" rel="stylesheet" type="text/css" title="black">
Working Example