Overview
Cookie JS consists of three main functions:
-
setCookie()
to create and set the cookies settings -
getCookie()
to retrieve the data of a cookie -
deleteCookie()
to delete a cookie
Place this at the end of your HTML document if you haven't already:
<!--Cookie JS-->
<script src="https://cdn.jsdelivr.net/npm/web-cookies-js/main/cookie.min.js" crossorigin="anonymous"></script>
setCookie()
Creates a cookie with the name, value, and days until expiration set by the user
Syntax
setCookie(cookieName,
cookieValue,
daysUntilExpiration)
Parameters
-
cookieName
(string) takes in the name of the cookie -
cookieValue
(string) takes in the value assigned to the cookie name -
daysUntilExpiration
(number) takes in the number of days until the cookie is automatically deleted
Example
setCookie("cookie", "chocolate", 1)
This example sets a cookie named "cookie" with the value "chocolate" which expires one day from the time it was created
Click the button to set the cookie above
setCookieThe alert is there only for demo purposes
getCookie()
Returns the value (if any) that corresponds with the cookie name inputted by the user
Syntax
getCookie(cookieName)
Parameters
-
cookieName
(string) takes in the name of the cookie to return
Example
getCookie("cookie")
This example returns the value stored in the cookie named "cookie"
Click the button to get the value of "cookie"
getCookieThe alert should return "chocolate"
deleteCookie()
Deletes the cookie specified by the user
Syntax
deleteCookie(cookieName)
Parameters
-
cookieName
(string) takes in the name of the cookie of which to be deleted
Example
deleteCookie("cookie")
This example completely deletes the specified cookie before the expiration date
Click the button to delete "cookie"
deleteCookieThe alert box shouldn't say anything
Try pressing the getCookie button from before; it shouldn't return anything
Wow you actually read the documentation!
Now put your knowledge of Cookie JS to use by making awesome websites!