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.

Cœur
  • 34,719
  • 24
  • 185
  • 251
S16
  • 2,923
  • 7
  • 38
  • 61

1 Answers1

3

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.

Community
  • 1
  • 1
James Sumners
  • 14,055
  • 10
  • 57
  • 75
  • 1
    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 Apr 23 '10 at 18:55
  • I'm going to write a plugin that automates this task and post it as the answer, but thank you for these responses. – S16 Apr 26 '10 at 20:08