简体   繁体   中英

Why is cookie not getting deleted?

I have created a cookie through javascript. Which I want to clear out on logout. The site is running on SSL. Following is the JS code that I have used for the cookie creation and deletion purpose. Note that it is a plain cookie (non http, non secure and as simple as it can be). Below is the function which is creating the cookie, which creates cookie just fine, this cookie is a session cookie and all other details are parameters used to create this cookie.

login: function () {
"use strict";
var cookieKeys, cookieValues;
if (cookieExists(this.systemPolicyCookie)) {
    cookieKeys = this.activeUserCookieName + '=';
    cookieValues = 'Email=' + this.activeUserCookieValues.Email + '&';
    cookieValues += 'UserId=' + this.activeUserCookieValues.UserId;
    cookieKeys += encodeURIComponent(cookieValues) + cookieDelimiter;
    cookieKeys += 'domain=' + this.activeUserCookieDomain + cookieDelimiter + cookiePath;
    document.cookie = cookieKeys;
    document.location.href = document.location.protocol + '//' + document.location.host + '/User/Landing/';
} else {
    alert('System policies are missing, please login online.');
    document.location.href = document.location.protocol + '//' + document.location.host;
}

},

The following code block should delete the cookie created above, which doesn't work.

logout: function () {
"use strict";
var cookies = cookieArray(),
    name;
for (name in cookies) {
    if (name != null && (this.systemPolicyCookie.toString()
        .toLowerCase() !== name.toString()
        .toLowerCase())) {
        document.cookie = name + '="";-1; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT';
    }
}
document.location.href = document.location.protocol + '//' + document.location.host;

},

The cookieArray() function returns a list of all cookies, which is as follows:

var cookieArray = function () {
"use strict";
var cookies = {},
counter,
split,
nameValue;
if (document.cookie && document.cookie !== '') {
    split = document.cookie.split(';');
    for (counter = 0; counter < split.length; counter++) {
        nameValue = split[counter].split("=");
        nameValue[0] = nameValue[0].replace(/^ /, '');
        cookies[decodeURIComponent(nameValue[0])] = decodeURIComponent(nameValue[1]);
    }
}
return cookies;

Could you help me get the cookie deletion code working, otherwise I will have to get the browser closed (bad Ux)

What browser are you using??

As far as I know, IE caches the cookies, therefore they will be deleted until you restart your browser. I'm not sure if FireFox or Chrome do the same

Have you tried testing your code in Chrome and/or FireFox??

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM