Remembering a boxes state with a cookie.

A

anathema

Having a bit of trouble adapting some code for my use.

This is the desired outcome - a user clicks on a#tools-link. This shows
/ hides the div #tool-box and a cookie is set to remember it's status
so that when the user goes to another page the #tool-box retains their
choice.

The show / hide part is fine. It is the cookie stuff I can not get
right.

The function hideBoxes is what is messed up. It is supposed to look at
the cookie and if it is set to 'invisible', then it should set the
class of #tool-box to 'invisible'

The function is overkill for what I am doing. It was originally
designed to keep track of the multiple boxes open or closed states, but
I am only keeping track of 1 for now. I do not think I need any of the
var kids stuff. So How can i rewrite that function to do what I need?

Here is the code.

// These are behaviour.js rules

var myrules = {
'a#tools-link' : function(el){
el.onclick = function(){
if (Element.hasClassName('tool-box', 'invisible')) {
new Effect.BlindDown('tool-box');
Element.removeClassName('tool-box', 'invisible');
setCookie('tool-box', '', 365);
} else {
new Effect.BlindUp('tool-box');
Element.addClassName('tool-box', 'invisible');
setCookie('tool-box', 'invisible', 365);
}
return false;
}
}
};


// Cookie stuff

function hideBoxes() {

// Id names of all the "boxes"
boxIds = $("tool-box");

for (i = 0; i < boxIds.length; i++) {
if (boxIds) {
cookieValue = readCookie(boxIds.id);
if (cookieValue == 'invisible') {
var div = boxIds.getElementsByTagName('div');
Element.addClassName(div[0], 'invisible');
var kids = boxIds.childNodes;
for (j = 1; j < kids.length; j++) {
if (kids[j].id) {
Element.hide(kids[j]);
}
}
}
}
}
}

function setCookie(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 readCookie(name) {
var needle = name + "=";
var cookieArray = document.cookie.split(';');
for(var i=0;i < cookieArray.length;i++) {
var pair = cookieArray;
while (pair.charAt(0)==' ') {
pair = pair.substring(1, pair.length);
}
if (pair.indexOf(needle) == 0) {
return pair.substring(needle.length, pair.length);
}
}
return null;
}

// Register the behaviour rules and load a few functions onload.
Behaviour.register(myrules);
Behaviour.addLoadEvent(hideBoxes);
 
G

getburl

Hi anathema,

I have a class you might be interested for manipulating your cookies
posted at http://adambragg.blogspot.com

getburl
Having a bit of trouble adapting some code for my use.

This is the desired outcome - a user clicks on a#tools-link. This shows
/ hides the div #tool-box and a cookie is set to remember it's status
so that when the user goes to another page the #tool-box retains their
choice.

The show / hide part is fine. It is the cookie stuff I can not get
right.

The function hideBoxes is what is messed up. It is supposed to look at
the cookie and if it is set to 'invisible', then it should set the
class of #tool-box to 'invisible'

The function is overkill for what I am doing. It was originally
designed to keep track of the multiple boxes open or closed states, but
I am only keeping track of 1 for now. I do not think I need any of the
var kids stuff. So How can i rewrite that function to do what I need?

Here is the code.

// These are behaviour.js rules

var myrules = {
'a#tools-link' : function(el){
el.onclick = function(){
if (Element.hasClassName('tool-box', 'invisible')) {
new Effect.BlindDown('tool-box');
Element.removeClassName('tool-box', 'invisible');
setCookie('tool-box', '', 365);
} else {
new Effect.BlindUp('tool-box');
Element.addClassName('tool-box', 'invisible');
setCookie('tool-box', 'invisible', 365);
}
return false;
}
}
};


// Cookie stuff

function hideBoxes() {

// Id names of all the "boxes"
boxIds = $("tool-box");

for (i = 0; i < boxIds.length; i++) {
if (boxIds) {
cookieValue = readCookie(boxIds.id);
if (cookieValue == 'invisible') {
var div = boxIds.getElementsByTagName('div');
Element.addClassName(div[0], 'invisible');
var kids = boxIds.childNodes;
for (j = 1; j < kids.length; j++) {
if (kids[j].id) {
Element.hide(kids[j]);
}
}
}
}
}
}

function setCookie(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 readCookie(name) {
var needle = name + "=";
var cookieArray = document.cookie.split(';');
for(var i=0;i < cookieArray.length;i++) {
var pair = cookieArray;
while (pair.charAt(0)==' ') {
pair = pair.substring(1, pair.length);
}
if (pair.indexOf(needle) == 0) {
return pair.substring(needle.length, pair.length);
}
}
return null;
}

// Register the behaviour rules and load a few functions onload.
Behaviour.register(myrules);
Behaviour.addLoadEvent(hideBoxes);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top