Contents
Cookies are nice. Cookies are a great way to store data about a user on their machine, which you can retrieve whenever you want and perfom some action on it. For example, you can use cookies to remember preferences from the user, displaying their name, or show the last time they visited the website.
You can extend the life of a cookie beyond the current browser session by setting an expiration date and saving the expiry date within the cookie. This can be done by setting the ‘expires’ attribute to a date and time.
How to find the value of a cookie in JavaScript?
If you want to find the value of one specified cookie, you must write a JavaScript function that searches for the cookie value in the cookie string. In the example to follow, we will create a cookie that stores the name of a visitor.
How do you set the expiration date on cookies?
Sometimes, you want cookies to remain even after they have ended the connection. To accomplish this, you have to set a custom expiration date. Setting the expiration date on cookies is as simple as giving the cookie a key/value pair where the key is expires and the value is when you want that cookie to expire.
The parameters of the function above are the name of the cookie (cname), the value of the cookie (cvalue), and the number of days until the cookie should expire (exdays). The function sets a cookie by adding together the cookiename, the cookie value, and the expires string.
You can also use Cookie in the client-side script without interacting with the server. JavaScript Cookie is the best option to store data in the web pages without using a server-side script. You can store, read, update, and delete data without interacting with the server.