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?
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?
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]
.
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.
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