我已经通过javascript创建了一个cookie。我想在退出时清除。该网站正在SSL上运行。以下是我用于cookie创建和删除目的的JS代码。请注意,它是一个纯cookie(非http,非安全且尽可能简单)。下面是创建cookie的功能,它可以很好地创建cookie,这个cookie是一个会话cookie,所有其他细节都是用来创建这个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;
}
},
以下代码块应删除上面创建的cookie,但不起作用。
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;
},
cookieArray()函数返回所有cookie的列表,如下所示:
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;
你能帮我把cookie删除代码搞定,否则我将不得不关闭浏览器(糟糕的Ux)
答案 0 :(得分:0)
你使用的浏览器是什么?
据我所知,IE会缓存cookie,因此在您重新启动浏览器之前,它们将被删除。我不确定FireFox或Chrome是否也这样做
您是否尝试过在Chrome和/或FireFox中测试代码?