Cookies enable the storage of user information on web pages.
Cookies are small pieces of data stored in text files on your computer.
After a web server sends a page to a browser and the connection is closed, the server loses all information about the user.
Cookies were created to address the problem of “how to remember information about the user”:
Cookies are stored as name-value pairs, such as:
| username = John Doe |
When a browser requests a web page from a server, the cookies associated with that page are included in the request. This allows the server to access the data needed to “remember” information about the user.
| None of the examples below will function if your browser has disabled support for local cookies. |
JavaScript can create, read, and delete cookies using the document.cookie property.
A cookie can be created in JavaScript like this:
| document.cookie = “username=John Doe”; |
You can specify an expiry date (in UTC time). By default, the cookie is removed when the browser is closed.
| document.cookie = “username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC”; |
By using a path parameter, you can specify the path to which the cookie belongs. By default, the cookie is associated with the current page.
| document.cookie = “username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/”; |