How to Set, Get and Delete Cookies using Vanilla JavaScript
This tutorial by Fineshop Design explains how to set, get, and delete cookies using Vanilla JavaScript through reusable functions. It includes code examples for managing cookies, adding configurations like expiration and security, and handling data with ease. The tutorial also provides a ready-to-use JavaScript library for cookie operations.
Key Features:
- Functions:
cookie.set(key, value, [config])
– Sets a cookie.
cookie.get(key)
– Retrieves a cookie value.
cookie.delete(key)
– Deletes a cookie.
- Additional methods include
getAll()
, has()
, and clear()
.
Examples:
- Setting a cookie with expiration:
cookie.set("user", JSON.stringify({ name: "Deo" }), { secure: true, "max-age": 3600 });
- Retrieving a cookie:
const user = JSON.parse(cookie.get("user"));
- Deleting a cookie:
cookie.delete("user");
Additional Resources:
The site also offers tutorials on JavaScript features like lazy loading, countdown timers, contact forms, and Progressive Web Apps (PWAs) for Blogger sites.
Note:
Fineshop Design is no longer actively maintained, so users should verify the code’s compatibility before use.