Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete all cookies with JavaScript? [duplicate]

I have written code to save cookies in JavaScript. Now I need to clear the cookies irrespective of values that I assigned.

Are there any script modules to delete all cookies that were generated by Javascript?

My Sample Code:

document.cookie = 'ppkcookie2=another test; expires=Fri, 3 Aug 2001 20:47:11 UTC; path=/'  function createCookie(name,value,days) {     if (days) {         var date = new Date();         date.setTime(date.getTime()+(days*24*60*60*1000));         var expires = "; expires="+date.toGMTString();     }     else var expires = "";     document.cookie = name+"="+value+expires+"; path=/"; }  function readCookie(name) {     var nameEQ = name + "=";     var ca = document.cookie.split(';');     for(var i=0;i < ca.length;i++) {         var c = ca[i];         while (c.charAt(0)==' ') c = c.substring(1,c.length);         if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);     }     return null; }  function eraseCookie(name) {     createCookie(name,"",-1); } 

How else could I clear all of the cookies?

Will there will be any problems when I test the code on the webserver?

like image 923
venkatachalam Avatar asked Feb 27 '09 15:02

venkatachalam


People also ask

How do you clear all the cookies in JS?

We can clear all cookies with JavaScript by setting the expiry date of each cookie to a date and time that's earlier than the current time. We create the deleteAllCookies function that splits the document. cookie string. Then we loop through each cookie within the cookies array.

Can we manipulate cookies using JavaScript?

JavaScript can also manipulate cookies using the cookie property of the Document object. JavaScript can read, create, modify, and delete the cookies that apply to the current web page.

Can JavaScript access all cookies?

You cannot. By design, for security purpose, you can access only the cookies set by your site.

What is duplicate cookie?

Description: Duplicate cookies setThe response contains two or more Set-Cookie headers that attempt to set the same cookie to different values. Browsers will only accept one of these values, typically the value in the last header. The presence of the duplicate headers may indicate a programming error.


1 Answers

There is no 100% solution to delete browser cookies.

The problem is that cookies are uniquely identified by not just by their key "name" but also their "domain" and "path".

Without knowing the "domain" and "path" of a cookie, you cannot reliably delete it. This information is not available through JavaScript's document.cookie. It's not available through the HTTP Cookie header either!

However, if you know the name, path and domain of a cookie, then you can clear it by setting an empty cookie with an expiry date in the past, for example:

function clearCookie(name, domain, path){     var domain = domain || document.domain;     var path = path || "/";     document.cookie = name + "=; expires=" + +new Date + "; domain=" + domain + "; path=" + path; }; 
like image 66
jb. Avatar answered Sep 19 '22 11:09

jb.