我通过 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)