1

Hi i am trying to make code for clear cookies in jquery onclick of logout button but didn't get solution

 function logout()
{
 document.cookie = 'Visit=; expires='+new Date(0).toUTCString() +'; path=/FinalVertozz/';
 window.location='../login.html'; 

}

cookies details

Name: Visit Content: 09850227123455130 Domain: localhost Path: /FinalVertozz Send for: Any kind of connection Accessible to script: Yes Created: Saturday, October 4, 2014 11:25:45 AM Expires: When the browsing session ends

  • http://stackoverflow.com/questions/1458724/how-to-set-unset-cookie-with-jquery/ check this one and try to reuse the function – Vignesh Pichamani Oct 04 '14 at 06:09
  • This issue is the path. If you remove it things will work (see my answer and example below). –  Oct 04 '14 at 06:13

3 Answers3

1

Can you use simple javascript. It's easy.

function ClearCookies()
{
   var cookiesCollection = document.cookie.split(";");
   for (var i = 0; i < cookiesCollection .length; i++) 
    {
    var cookieName = cookiesCollection [i];
    var pos= cookieName.indexOf("=");
    var name = pos> -1 ? cookieName.substr(0, pos) : cookieName;
    document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
    }
}
0

This issue is the path. If you remove it things will work.

document.cookie = 'Visit=; expires='+new Date(0).toUTCString();

Also, you're not using any jQuery in the above example. The question may be better stated as "remove cookies in JavaScript on logout button".

0

From the reference of this question and answer from @Russ Cam How do I set/unset cookie with jQuery? Use this function to reuse

function createCookie(name, value, days) {
    var expires;

    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    } else {
        expires = "";
    }
    document.cookie = escape(name) + "=" + escape(value) + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = escape(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 unescape(c.substring(nameEQ.length, c.length));
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

Hope it may help :)

Community
  • 1
  • 1
Vignesh Pichamani
  • 7,950
  • 22
  • 77
  • 115