Javascript Cookies

JavaScript Cookies

Cookie is a piece of data which is sent from a website and stored locally by the user's browser. Cookie is needed because HTTP is stateless protocol. Today most of the websites uses cookie. Cookies is the most efficient method of remembering and tracking preferences and other information required for better visitor experience or site statistics.

How it works

The structure of cookie is simple it consist several key-value pairs. Pairs are separated by semicolons, while the equal sign character separates the key from its value. Cookies are plain text data record of 5 variable-length fields-
  • Expires
  • Domain
  • Path
  • Secure
  • Name=value

Storing cookies

The easiest way to store cookie is to assign string value to the document. Cookie object looks like this:
document.cookie = "key1=value1;key2=value2; expires=date";