Cookies provide a way for a client and server to interact and pass information via HTTP. This enables the client to store state information despite using HTTP which is a stateless protocol. When a browser requests a web page from a server, the server services the request and “forgets” about the visit. But it passes some information to the user’s browser. The browser stores the information in the form of key=value pairs and also manages this information.
- Cookie information is passed in the HTTP request header during every subsequent visit to the domain from the user’s browser.
- Information like login details, consent, other preferences are used to enhance and customize the user experience.
HTTP cookies expire, the date and time are specified in the “expires” attribute. As a result, the browser automatically deletes the cookies when the date and time exceed the expiration date (and time). As this attribute is configurable*, it is possible to delete all the cookies by setting the “expiry” to any value that has already passed.
The cookie property of the current document is used to modify the attributes of the cookies buy using HTML DOM cookie Property. The document.cookie returns a single string of all the cookies separated by semicolons associated with the current document.
document.cookie = "key=value";