/    Sign up×
Community /Pin to ProfileBookmark

Problem with cookies for a style switcher

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;
}
[/code]

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @cturcotte224 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...