views:

220

answers:

1

My goal here is to cut down the number of cookies I'm using to store things like persistent states into a single cookie by setting and retrieving multiple key/value pairs within a single cookie using an array.

I'm mostly interested in knowing if someone else has already done this and has written a plugin for it, as I would think this has been done before.

+2  A: 

You could serialize the object into a JSON string. That would make it super simple to re-load the object. See question 191881 for information on how to serialize an object to JSON in JavaScript.

jsumners
This is the usual approach. Note you'll still need to encode the JSON string for use in a cookie (eg. using `encodeURIComponent`), because JSON may contain semicolons, Unicode and other charactes that won't fit into a cookie.
bobince
I didn't think about that. Good point.
jsumners
I'm going to write a plugin that automates this task and post it as the answer, but thank you for these responses.
Greg-J