Cookie Parameters

As mentioned above, cookies have several parameters:

  • name-value pairs, which are mandatory

  • expiration date/times, which are optional

  • paths

  • domains

  • secure

Let's look at all of these in turn.

Name-Value Pairs

Name-value pairs are the only piece of a cookie that needs to exist. For example, the world's simplest cookie can be “a=1”. That's it, and that's enough.

The value can't have any spaces, commas, or semicolons, so it can be a little challenging to store more than one value in the name-value pair. One way to work around this is to URL-encode the value, so that “Hi there”, an illegal value, would become “Hi%20there”, a perfectly legal value.

This restriction means that arrays and objects can't be directly placed into a cookie. However, ...

Get Advanced JavaScript™: Insights and Innovative Techniques now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.