How do I delete the contents of cookies? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

How do I delete the contents of cookies?

I at least need to know where the lesson for cookies is located.

14th Mar 2017, 3:49 PM
Theprogrammers
Theprogrammers - avatar
1 Answer
+ 9
Overwrite them with a date in the past. Do you mean localStorage and sessionStorage, because we're slightly talking about something different here? Anyway here's the JS for creating a cookie first: function create_cookie(cookie_name, cookie_value = "", cookie_expires, cookie_path = "/", cookie_new = true){ var now_date = new Date(); if(cookie_new){ now_date.setTime(now_date.getTime() + (cookie_expires * 24 * 60 * 60 * 1000)); }else{ now_date.setTime(now_date.getTime() - (cookie_expires * 24 * 60 * 60 * 1000)); } var cookie_end = "expires=" + now_date.toUTCString(); document.cookie = cookie_name + "=" + cookie_value + ";" + cookie_end + ";path=" + cookie_path; } create_cookie("myCookie", "myCookieValue", 30); That would create a cookie named myCookie with a value myCookieValue which will expire in 30 days. I've left the path parameter empty as it will set the cookie to it's default or current path/all, and the last parameter to it's default true - new cookie. To delete this cookie just overwrite it like so, and you wouldn't even need to pass a cookie value to it. The false parameter tells the function that your deducting time from the current to set it in the past: create_cookie("myCookie",,1,,false); I recommend you read this: https://www.w3schools.com/js/js_cookies.asp
14th Mar 2017, 10:44 PM
Mark Foxx
Mark Foxx - avatar