PDA

View Full Version : Help with multiple style switcher scripts



ojoonline
08-18-2011, 03:33 AM
Hi,

I have a site that uses this switcher code, as below. I want to add to another related website. The problem is they over-write the cookies from one site to the other. For example, if I'm on one site and I choose a style and then when I go to the other site that site is looking at the same cookie so loads the wrong style.

I've tried renaming all my stylesheets but what happens is the other site just doesn't load one or look for the default because the cookie already exists.

I've tried renaming all the functions too but that doesn't work (because I don't know what I'm doing!).

Thanks for your help.

jo



<link rel="stylesheet" type="text/css" href="style_100.css" title="style00" />
<link rel="alternate stylesheet" type="text/css" href="style_101.css" title="style01" />
<link rel="alternate stylesheet" type="text/css" href="style_102.css" title="style02" />
<link rel="alternate stylesheet" type="text/css" href="style_103.css" title="style03" />
<link rel="alternate stylesheet" type="text/css" href="style_104.css" title="style04" />
<link rel="alternate stylesheet" type="text/css" href="style_105.css" title="style05" />





function setActiveStyleSheet2(title) {
var i, a, main;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
a.disabled = true;
if(a.getAttribute("title") == title) a.disabled = false;
}
}
}

function getActiveStyleSheet2() {
var i, a;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
}
return null;
}

function getPreferredStyleSheet2() {
var i, a;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style") != -1
&& a.getAttribute("rel").indexOf("alt") == -1
&& a.getAttribute("title")
) return a.getAttribute("title");
}
return null;
}

function createCookie2(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie2(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

window.onload = function(e) {
var cookie = readCookie2("style");
var title = cookie ? cookie : getPreferredStyleSheet2();
setActiveStyleSheet2(title);
}

window.onunload = function(e) {
var title = getActiveStyleSheet2();
createCookie2("style", title, 365);
}

var cookie = readCookie2("style");
var title = cookie ? cookie : getPreferredStyleSheet2();
setActiveStyleSheet2(title);