
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to set multiple cookies in JavaScript?
With JavaScript, to set more than one cookie, set document.cookie more than once using the; separator.
Example
You can try to run the following code to set multiple cookies −
<!DOCTYPE html> <html> <head> <script> var num=1; function addCookie() { document.cookie=num+"="+num; num++; } function listCookies() { var result = document.cookie; document.getElementById("list").innerHTML=result; } function removeCookies() { var res = document.cookie; var multiple = res.split(";"); for(var i=0;i<multiple.length;i++) { var key = multiple[i].split("="); document.cookie=key[0]+"=; expires=Thu, 01 Jan 1970 00:00:00 UTC"; } } </script> </head> <body> <button onclick='addCookie()'>ADD</button><br> <button onclick='listCookies()'>LIST COOKIES</button><br> <button onclick='removeCookies()'>REMOVE</button> <h1>Cookies List</h1> <p id="list"></p> </body> </html>
- Related Questions & Answers
- How to use JavaScript to set cookies for multiple pages?
- How to set named cookies in JavaScript?
- How to set cookies expiry date in JavaScript?
- How to set cookies in ReactJS?
- How to set cookies to expire in 1 hour in JavaScript?
- How to set cookies to expire in 30 minutes in JavaScript?
- How to use JavaScript to set cookies for homepage only?
- How to create cookies in JavaScript?
- How to delete cookies in JavaScript?
- How to use JavaScript to set cookies for a specific page only?
- How do you set cookies in the JSP?
- How to clear all cookies with JavaScript?
- How to store large data in JavaScript cookies?
- How to create domain-based cookies using JavaScript?
- How to detect that JavaScript Cookies are disabled?
Advertisements