473,698 Members | 2,346 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Remembering a boxes state with a cookie.

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.hasCla ssName('tool-box', 'invisible')) {
new Effect.BlindDow n('tool-box');
Element.removeC lassName('tool-box', 'invisible');
setCookie('tool-box', '', 365);
} else {
new Effect.BlindUp( 'tool-box');
Element.addClas sName('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[i]) {
cookieValue = readCookie(boxI ds[i].id);
if (cookieValue == 'invisible') {
var div = boxIds[i].getElementsByT agName('div');
Element.addClas sName(div[0], 'invisible');
var kids = boxIds[i].childNodes;
for (j = 1; j < kids.length; j++) {
if (kids[j].id) {
Element.hide(ki ds[j]);
}
}
}
}
}
}

function setCookie(name, value,days) {
if (days) {
var date = new Date();
date.setTime(da te.getTime()+(d ays*24*60*60*10 00));
var expires = ";expires="+dat e.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.len gth;i++) {
var pair = cookieArray[i];
while (pair.charAt(0) ==' ') {
pair = pair.substring( 1, pair.length);
}
if (pair.indexOf(n eedle) == 0) {
return pair.substring( needle.length, pair.length);
}
}
return null;
}

// Register the behaviour rules and load a few functions onload.
Behaviour.regis ter(myrules);
Behaviour.addLo adEvent(hideBox es);

Jan 23 '07 #1
1 1549
Hi anathema,

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

getburl

anathema wrote:
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.hasCla ssName('tool-box', 'invisible')) {
new Effect.BlindDow n('tool-box');
Element.removeC lassName('tool-box', 'invisible');
setCookie('tool-box', '', 365);
} else {
new Effect.BlindUp( 'tool-box');
Element.addClas sName('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[i]) {
cookieValue = readCookie(boxI ds[i].id);
if (cookieValue == 'invisible') {
var div = boxIds[i].getElementsByT agName('div');
Element.addClas sName(div[0], 'invisible');
var kids = boxIds[i].childNodes;
for (j = 1; j < kids.length; j++) {
if (kids[j].id) {
Element.hide(ki ds[j]);
}
}
}
}
}
}

function setCookie(name, value,days) {
if (days) {
var date = new Date();
date.setTime(da te.getTime()+(d ays*24*60*60*10 00));
var expires = ";expires="+dat e.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.len gth;i++) {
var pair = cookieArray[i];
while (pair.charAt(0) ==' ') {
pair = pair.substring( 1, pair.length);
}
if (pair.indexOf(n eedle) == 0) {
return pair.substring( needle.length, pair.length);
}
}
return null;
}

// Register the behaviour rules and load a few functions onload.
Behaviour.regis ter(myrules);
Behaviour.addLo adEvent(hideBox es);
Jan 23 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
2467
by: | last post by:
Please help. After a number of wrong turns and experiments I need advice on login management system to secure our web pages without inconveniencing our visitors or our internal staff. What I need: A system whereby the user only has to register ONCE and he will have automatic entry to ANY page without havinto to RE-LOGIN even if he comes in
0
1716
by: nbdy9(nospam) | last post by:
I have a ASP.Net website (mobile). From time to time the browser popup the following message. Anyway to catch the error and redirect to my start page? saying, login.aspx? Thanks. ======================================================================= Server Error in '/mobSec' Application. The page requires session state that is no longer available. Either the session has expired, the client did not send a valid session cookie, or the...
5
1402
by: Ryan Ternier | last post by:
Hey! We need a solution for a problem. We're designing a site where users hate... absolutly hate... with a passion to sign in with a user name / password system. They want to sign in once, and that's it. User's are all behind an internal IP so we can't use the old method of remembering IP's as that would mess up user preferences. We really don't like using cookies either. Is there another method we could use? Encrypted XML on the...
2
1506
by: WJ | last post by:
I am setting the Session State TimeOut to 54 minutes (20 is default) in the Web.Config. I use SQL Server to persist the session states & ID. As long as I am remaining in the web sites, I see all the data persisted from page to page. However, once I quit the browser (MS/IE 6.1) and got backin within 2 seconds, the persisted data no long existed. My impression on the "timeout" paramter is that as long as I come back to my site within the...
2
2480
by: jakk | last post by:
Iam storing the session state in SQL Server. The Session gets stored in the SQL Server temp tables ( I can see some values in the two tables), but the session doesnt seem to timeout. We have a situation where we have to differentiate between logout and session expiration and since the Session_End event doesnt get fired for out-of-proc sessions, I added tthe two functions public void Application_PreRequestHandlerExecute(Object sender,...
2
1193
by: Bill Manring | last post by:
I have an ASP.NET application with many different pages. On one particular page, I would like it to be restored to its previous state if a user navigates away (within the application) and then returns to that page - as if they had used the "Back" button in IE to get back to the page. I tried saving the viewState of the page in a session variable, but that failed when I tried to restore it on subsequent Page_Load events. Is there...
11
7910
by: Joseph Geretz | last post by:
I've been looking at two approaches for the maintenance of Session state for a Web Service application. One approach uses the old familiar Session object which I've used in the past for Web applications. As far as I can see, the Session approach is non-standard since Web Services are supposed to be agnostic with respect to their clients. It seems that cookies are outside the Web Service standard; therefore, such a Web Service application...
11
2876
by: Nospam | last post by:
I don't know what it is I am doing wrong, I am trying to get the menus to either expand or contract based on their previous states, i.e if already expanded if clicked again contract, and if contracted, expand, so far it doesn't work for the about and services link(but works for the expand all and contract all links), any help would be greatly appreciated: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"...
0
2095
by: Homer J. Simpson | last post by:
A few weeks ago I asked for suggestions on how to persist a tree's node state to cookies, without causing postbacks on each click in the tree. I had a single .aspx file, with a row of buttons on top of the screen, and a tree control below (with EnableViewState set to true). I let the user expand/collapse away to his hearts content without needless roundtrips. When the user clicked on a button at the top, I'd reload the page, check...
0
8676
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8608
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8898
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7734
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6524
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.