1

How Can I delete cookie contains string

displayName

in name? it is only one cookie with this string, but I don't know all name, only the part?

faszynski
  • 419
  • 3
  • 11
  • 26
  • I think you cannot do this. If you can create a cookie name dynamically so you can set the cookie's value to null with jquery: `$.cookie("_constantStringName_" + variable, null);` – Felipe Oriani Nov 27 '12 at 11:35

3 Answers3

3

If you don't know the full name of the cookie, you could easily find out by inspecting the entire cookie collection:

var names = document.cookie.split(';')
   .map(function(c) { return c.split('=')[0]; })
   .filter(function(c) { return c.indexOf('displayName') > -1; });

Here, names would contain all the cookies with displayName in their name. If you're sure that'll only ever be one, go ahead and delete names[0].

David Hedlund
  • 128,221
  • 31
  • 203
  • 222
1
var cookies = document.cookie.split(";");
for (i = 0; i <= cookies.length; i++) {
    if (cookies[i].indexOf(name) != -1)
        document.cookie = name + "=" + value + "; 01 Jan 1970 00:00:01 GMT; path=/";
}

This deletes all cookies that have the content of name in their name. This is pure Javascript, no JQuery involved.

The reason why I set the date to 01 Jan 1970 00:00:01 GMT is, because we already passed that date and so, the browser will delete the cookie.

looper
  • 1,929
  • 23
  • 42
0

delete cookie:

function Delete_Cookie( name, path, domain ) 
{
   if ( Get_Cookie( name ) )
   document.cookie=name+"="+((path) ? ";path="+path:"")+((domain)?";domain="+domain:"") +
                                   ";expires=Thu, 01 Jan 1970 00:00:01 GMT";
}

or

function del_cookie(name)
{
    document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}

source: javascript - delete cookie

Community
  • 1
  • 1
mjimcua
  • 2,781
  • 3
  • 27
  • 47