Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple keys for javascript cookie

Status
Not open for further replies.

dinosf

Technical User
Dec 27, 2002
11
0
0
Hey guys -

Sorry for the basic question, but my javascript knowledge is small, I've got an app that I'm developing and need to use a cookie the document.cookie(name=value) works fine, but I'm not sure of the syntax to assign multiple keys to the same cookie (e.g. is is document.cookie("name"), key1=value). It's primarily for an economy of code that I'm asking, I could use 3 cookies for my purposes, but 1 with 3 name value pairs seems more efficient. Thank you for your time in reading this.
 
The cookie data could be packed in such a way as to yield the same as 3 cookies... use a known separator and you're sorted.

Code:
var val = "key1[!]=[/!]data1[!]|[/!]key2[!]=[/!]data2[!]|[/!]key3[!]=[/!]data3";
document.cookie(name=val);

You would access the main cookie using "name" and then take the resulting value and split it on the [!]|[/!] character to give you 3 name/value pairs. Finally you could split an individual pair on the [!]=[/!] character.

Hope this makes some sense. The trick is finding a separator that doesn't appear in your dataset [smile]

Cheers,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
Perfect. Thank you so much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top