I am working on a project for school. I have built a style switch and I am trying to set and retrieve cookies for it. I am able to set the cookie, it shows up in Cookie Editor on FF, however I can’t figure out how to parse the info out of the cookie and use it to set up the page upon loading. I am fairly new to javascript and cookies in particular. Any help would be much appreciated.
[code]
var allStyles = new Array();
function makeStyleButton()
{
var allLinks = document.getElementsByTagName(“link”);
for(var i = 0; i<allLinks.length; i++)
{
if((allLinks[i].rel == “stylesheet” ||
allLinks[i].rel == “alternate stylesheet”) &&
allLinks[i].rel.title !=””)
{
allStyles.push(allLinks[i]);
}
}
var styleBox = document.createElement(“div”);
for(var i =0; i<allStyles.length; i++)
{
var count = 0;
count++;
if(allStyles[i].rel == “stylesheet”)
{
allStyles[i].disabled = false;
}
else
{
allStyles[i].disabled = true;
}
styleButton = document.createElement(“input”);
styleButton.type = “button”;
styleButton.value = allStyles[i].title + ” view”;
styleButton.title = allStyles[i].title;
styleButton.style.width = “125px”;
styleButton.style.fontSize = “12px”;
styleButton.onclick = changeStyle;
styleBox.appendChild(styleButton);
}
styleBox.style.width = “125px”;
styleBox.style.cssFloat = “right”;
styleBox.style.styleFloat = “right”;
styleBox.style.margin = “5px 5px 10px 10px”;
var sourceDoc = document.getElementById(“navbar”);
sourceDoc.appendChild(styleBox, sourceDoc);
}
function changeStyle()
{
var data=””;
for(var i =0; i < allStyles.length; i++)
data += (allStyles[i].disabled = allStyles[i].title !=this.title) ? 0:1;
setCookie(‘cssStyle’, data, 30);
}
function setCookie(name, value, expiry)
{
var date = new Date();
date.setTime(date.getTime()+(expiry*24*60*60*1000));
expiry = “; expires=”+date.toGMTString();
document.cookie = name + “=” + value + expiry + “; path=/”;
}
function getCookie(searchName)
{
if (document.cookie.length>0)
{
var nameValuePair, cookieName, cookieValue
var pairs = document.cookie.split(‘;’);
for(var i=0; i<pairs.length; i++)
{
nameValuePair = pairs[i].split(‘=’);
cookieName = nameValuePair[0].replace(/^s+|s+$/g,”);
cookieValue = nameValuePair[1].replace(/^s+|s+$/g,”);
if(cookieName == searchName) { return cookieValue; }
}
}
return false;
}