Javascript?
Javascript?
How to Manage
Cookies in
JavaScript
Quick & Easy Guide for Beginners!
Slim toumi
Slim toumi
JavaScript tips
What is cookies ?
Cookies are small pieces of data stored in a user's browser to
remember information, like login status or preferences, between
visits. Here’s a guide to working with cookies in JavaScript!
Creating cookies
Note:
Sets a cookie with a key of username and a value of JohnDoe.
Default expires when the session ends (when the browser is closed).
Slim toumi
Slim toumi
JavaScript tips
Note:
The expires attribute takes a date in UTC string format.
Note:
Expires in 7 days (604800 seconds)
If neither expires nor max-age is set, the cookie will be a session cookie
and will be deleted when the browser is closed.
Slim toumi
Slim toumi
JavaScript tips
Note:
path=/: Makes the cookie accessible on all pages of the website.
Slim toumi
Slim toumi
JavaScript tips
Reading Cookies
To read cookies, access document.cookie, which returns a
string of all cookies in key-value pairs.
Usage:
Slim toumi
Slim toumi
JavaScript tips
Deleting Cookies
To delete a cookie, set its expiration date to a past date.
Best Practices
Limit usage: Keep cookies small to reduce load time.
Secure data: Only store non-sensitive data, and use
HTTPOnly and Secure attributes when possible.
Slim toumi
Slim toumi
JavaScript tips
Slim toumi
Slim toumi