Learn About Cookies
Cookies are small pieces of data stored on the user's computer by the web browser while browsing a website. They are used to remember information about the user, such as login status, preferences, and other session-related data. Here are some key concepts about cookies that a user should learn:
- Setting a Cookie:
- Cookies are set using the
document.cookie
property in JavaScript.
- A cookie consists of a name-value pair, and optionally, attributes like expiration date, path, domain, and secure flag.
- Expiration Date:
- Cookies can have an expiration date, after which they are automatically deleted by the browser.
- If no expiration date is set, the cookie is considered a session cookie and is deleted when the browser is closed.
- Path Attribute:
- The path attribute specifies the URL path that must exist in the requested URL for the browser to send the cookie header.
- By default, the path is set to the current document location.
- Reading a Cookie:
- Cookies can be read using
document.cookie
, which returns all cookies in a single string.
- Parsing this string is necessary to extract individual cookie values.
- Deleting a Cookie:
- To delete a cookie, set its expiration date to a past date.