Reputation: 7639
i write a small javascript function
function getCookie(cname) {
var name = cname + "=";
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);
if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
}
return "";
}
function deleteCookie(key)
{
// Delete a cookie by setting the date of expiry to yesterday
date = new Date();
date.setDate(date.getDate() -1);
document.cookie = escape(key) + '=;expires=' + date;
}
in my console .i set a cookie by document.cookie = "Next=true"; i called getCookie('Next') .its returning true i called deleteCookie('Next') and then called getCookie('Next') still its returning true.can anyone please tell why its not deleting cookies ??
Upvotes: 2
Views: 134
Reputation: 3878
Also setting the cookie's domain worked for me:
document.cookie = 'cookiename=; Max-Age=0; path=/; domain=.example.com';
Upvotes: 0
Reputation: 78
Try this one:
function deleteCookie(key) {
document.cookie =
encodeURIComponent(key) +
"=deleted; expires=" +
new Date(0).toUTCString();
}
Upvotes: 2
Reputation: 2437
To delete a cookie with JQuery
//To set a cookie
02
$.cookie('the_cookie', 'the_value');
03
04
//Create expiring cookie, 7 days from then:
05
$.cookie('the_cookie', 'the_value', { expires: 7 });
06
07
//Create expiring cookie, valid across entire page:
08
$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });
09
10
//Read cookie
11
$.cookie('the_cookie'); // => 'the_value'
12
$.cookie('not_existing'); // => null
13
14
//Delete cookie by passing null as value:
15
$.cookie('the_cookie', null);
16
17
// Creating cookie with all availabl options
18
$.cookie('myCookie2', 'myValue2', { expires: 7, path: '/', domain: 'example.com',
19
secure: true, raw: true });
Upvotes: 1