Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

storing cookie data in a hash

Posted on 2008-06-20
5
1,087 Views
Last Modified: 2010-04-21
Hello Experts,
I am saving id and title in the cookie using jQuery cookie plugin separated by ^
$.cookie('slist' , $.cookie('stlist') +"^" + title);
It is working fine but the problem I am having is when I want to retrieve the value either to show or delete particular record from the cookie. I have to make exact match of how the cookie been saved.
All I wanted to know If I can  store the cookie in something like a hash array, every id keeps the title in it? Please can someone help.
I hope you understand the problem here?
Please advice
Thanks




saveList(theId+":"+myTitle);
 
function saveList(title){
   if( $.cookie('slist') != null){
		$.cookie('slist' , $.cookie('stlist') +"^" + title);
   }else{
       $.cookie('slist', title );
   }
   return;
}
 
this is how it is getting saved in the cookie
"i1234%3AMichel%2C%20David%5E"
for 
id = i1234
title = Michel David

Open in new window

0
Comment
Question by:newbie27
  • 3
  • 2
5 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 21832904
What is separating the cookie "records". The "^" seems to delimit the value for a single record - ex: Joe^Student
But what I am asking is if you are storing Sally^Professor, when you retrieve slist, what does it look like? Is it:
Joe^Student,Sally^Professor?
0
 
LVL 8

Author Comment

by:newbie27
ID: 21832962
Hello Hielo,
Attached are the 3 functions I am using to handle cookie values.
The problem I am having in retrieving its value, I always have to make the exact match, as  the data been stored in the cookie.

for ex: e12334324234:Joe Student ^ e2324232342:Sally Professor

function saveShortList(title){
   if( $.cookie('shortlist') != null){
		$.cookie('shortlist' , $.cookie('shortlist') +"^" + title);
   }else{
       $.cookie('shortlist', title );
   }
   return;
}
 
function getShortList(){
	var ret = new Array();
	if( $.cookie('shortlist') )
	{
		var data = $.cookie('shortlist').split("^");
		for(var i=0; i<data.length; i++)
		{
			var subdata = data[i].split(":"); //" :" your separator
			ret.push({id:subdata[0], title:subdata[1]});
		}
	}
	return ret;
}
 
function displayShortList(){
	var myList = getShortList();
	for(var x=0; x<myList.length; x++)
	{	
		if(myList[x].id != 'null'){	
			$('#shortlist').append('<li id="' + myList[x].id + '_li" title="'+ myList[x].title +'" style="background-image:url(\'\');padding-left:0"><a href="#" class="dynAdded" onclick="removeItem(this); return false;" title="Remove">&nbsp;[-]</a>' + myList[x].title + ' </li>');				
		}			
	}
 
}

Open in new window

0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 21833043
getShortList() is returning an ARRAY. So if you are interested in  e2324232342:Sally Professor

You will need:
var myList =  getShortList();
for(var i=0; i < myList.length; ++i)
{
  if( myList[i].id=='e2324232342' )
   alert( myList[i].id + " " myList[i].title)
}
0
 
LVL 8

Author Closing Comment

by:newbie27
ID: 31469146
thanks
0
 
LVL 82

Expert Comment

by:hielo
ID: 21834669
You are welcome!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to deal with the query string 8 37
html Uncheck Checkbox 2 25
ASP.NET Content Page 3 25
Button on Table, name table1 not working 4 20
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question