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
  • 37,241
  • 25
  • 195
  • 267
S16
  • 2,963
  • 9
  • 40
  • 64

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,485
  • 10
  • 59
  • 77
  • 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