JavaScript设置并获取cookie?
我有一个保存cookies的函数JavaScript设置并获取cookie?,javascript,html,cookies,Javascript,Html,Cookies,我有一个保存cookies的函数 cookievalue= escape(document.passwordCheck.oldPassword.value) + ";"; document.cookie="oldCookie=" + cookievalue cookievalue= escape(document.passwordCheck.oldPassword.value) + ";"; document.cookie="newCookie=" + cooki
cookievalue= escape(document.passwordCheck.oldPassword.value) + ";";
document.cookie="oldCookie=" + cookievalue
cookievalue= escape(document.passwordCheck.oldPassword.value) + ";";
document.cookie="newCookie=" + cookievalue
如何检索oldCookie和newCookie的数据?来自:
发件人:
使用cookie库或其他函数获取值
使用cookie库或其他函数获取值
W3CSchool的功能是错误的。如果有多个cookie具有相同的后缀,则失败,如:
ffoo=bar; foo=baz
搜索foo时,它将返回ffoo的值,而不是foo
现在我要做的是:首先,您需要了解Cookie如何传输的语法。Netscape的原始规范只有副本可用,例如使用分号分隔多个cookie,而每个名称/值对具有以下语法:
名称=值
此字符串是一个字符序列,不包括分号、逗号和空格。如果需要在名称或值中放置此类数据,建议使用一些编码方法,例如URL样式%XX编码,但不定义或不需要编码
因此,在分号或逗号处拆分document.cookie字符串是一种可行的选择
除此之外,还规定Cookie由分号或逗号分隔:
虽然两者都允许,但逗号是首选的,因为它们是HTTP中列表项的默认分隔符
注意:为了向后兼容,Cookie头中的分隔符
是分号;处处服务器也应该接受逗号,
作为cookie值之间的分隔符,以实现将来的兼容性
此外,名称/值对还有一些进一步的限制,因为值也可以是引用字符串,如中所述:
因此,这两个cookie版本需要分别处理:
if (typeof String.prototype.trimLeft !== "function") {
String.prototype.trimLeft = function() {
return this.replace(/^\s+/, "");
};
}
if (typeof String.prototype.trimRight !== "function") {
String.prototype.trimRight = function() {
return this.replace(/\s+$/, "");
};
}
if (typeof Array.prototype.map !== "function") {
Array.prototype.map = function(callback, thisArg) {
for (var i=0, n=this.length, a=[]; i<n; i++) {
if (i in this) a[i] = callback.call(thisArg, this[i]);
}
return a;
};
}
function getCookies() {
var c = document.cookie, v = 0, cookies = {};
if (document.cookie.match(/^\s*\$Version=(?:"1"|1);\s*(.*)/)) {
c = RegExp.$1;
v = 1;
}
if (v === 0) {
c.split(/[,;]/).map(function(cookie) {
var parts = cookie.split(/=/, 2),
name = decodeURIComponent(parts[0].trimLeft()),
value = parts.length > 1 ? decodeURIComponent(parts[1].trimRight()) : null;
cookies[name] = value;
});
} else {
c.match(/(?:^|\s+)([!#$%&'*+\-.0-9A-Z^`a-z|~]+)=([!#$%&'*+\-.0-9A-Z^`a-z|~]*|"(?:[\x20-\x7E\x80\xFF]|\\[\x00-\x7F])*")(?=\s*[,;]|$)/g).map(function($0, $1) {
var name = $0,
value = $1.charAt(0) === '"'
? $1.substr(1, -1).replace(/\\(.)/g, "$1")
: $1;
cookies[name] = value;
});
}
return cookies;
}
function getCookie(name) {
return getCookies()[name];
}
来源:W3CSchool的功能错误。如果有多个cookie具有相同的后缀,则失败,如:
ffoo=bar; foo=baz
搜索foo时,它将返回ffoo的值,而不是foo
现在我要做的是:首先,您需要了解Cookie如何传输的语法。Netscape的原始规范只有副本可用,例如使用分号分隔多个cookie,而每个名称/值对具有以下语法:
名称=值
此字符串是一个字符序列,不包括分号、逗号和空格。如果需要在名称或值中放置此类数据,建议使用一些编码方法,例如URL样式%XX编码,但不定义或不需要编码
因此,在分号或逗号处拆分document.cookie字符串是一种可行的选择
除此之外,还规定Cookie由分号或逗号分隔:
虽然两者都允许,但逗号是首选的,因为它们是HTTP中列表项的默认分隔符
注意:为了向后兼容,Cookie头中的分隔符
是分号;处处服务器也应该接受逗号,
作为cookie值之间的分隔符,以实现将来的兼容性
此外,名称/值对还有一些进一步的限制,因为值也可以是引用字符串,如中所述:
因此,这两个cookie版本需要分别处理:
if (typeof String.prototype.trimLeft !== "function") {
String.prototype.trimLeft = function() {
return this.replace(/^\s+/, "");
};
}
if (typeof String.prototype.trimRight !== "function") {
String.prototype.trimRight = function() {
return this.replace(/\s+$/, "");
};
}
if (typeof Array.prototype.map !== "function") {
Array.prototype.map = function(callback, thisArg) {
for (var i=0, n=this.length, a=[]; i<n; i++) {
if (i in this) a[i] = callback.call(thisArg, this[i]);
}
return a;
};
}
function getCookies() {
var c = document.cookie, v = 0, cookies = {};
if (document.cookie.match(/^\s*\$Version=(?:"1"|1);\s*(.*)/)) {
c = RegExp.$1;
v = 1;
}
if (v === 0) {
c.split(/[,;]/).map(function(cookie) {
var parts = cookie.split(/=/, 2),
name = decodeURIComponent(parts[0].trimLeft()),
value = parts.length > 1 ? decodeURIComponent(parts[1].trimRight()) : null;
cookies[name] = value;
});
} else {
c.match(/(?:^|\s+)([!#$%&'*+\-.0-9A-Z^`a-z|~]+)=([!#$%&'*+\-.0-9A-Z^`a-z|~]*|"(?:[\x20-\x7E\x80\xFF]|\\[\x00-\x7F])*")(?=\s*[,;]|$)/g).map(function($0, $1) {
var name = $0,
value = $1.charAt(0) === '"'
? $1.substr(1, -1).replace(/\\(.)/g, "$1")
: $1;
cookies[name] = value;
});
}
return cookies;
}
function getCookie(name) {
return getCookies()[name];
}
来源:您是否尝试过搜索解决方案?重复:您不应该在Cookie中存储密码。您是否尝试过搜索解决方案?重复:您不应该在Cookie中存储密码。您不应该用大写字母启动非构造函数函数名。您不应该用大写字母启动非构造函数函数名不管大写字母与否,你都不应该撕毁其他答案。如果问题重复,请在注释中链接到现有问题。@JonathanLonowski Sure…:将从下一步开始执行!:不管你是否知道,你都不应该撕毁其他答案。如果问题重复,请在注释中链接到现有问题。@JonathanLonowski Sure…:将从下一步开始执行!:
attr = token
value = token | quoted-string
if (typeof String.prototype.trimLeft !== "function") {
String.prototype.trimLeft = function() {
return this.replace(/^\s+/, "");
};
}
if (typeof String.prototype.trimRight !== "function") {
String.prototype.trimRight = function() {
return this.replace(/\s+$/, "");
};
}
if (typeof Array.prototype.map !== "function") {
Array.prototype.map = function(callback, thisArg) {
for (var i=0, n=this.length, a=[]; i<n; i++) {
if (i in this) a[i] = callback.call(thisArg, this[i]);
}
return a;
};
}
function getCookies() {
var c = document.cookie, v = 0, cookies = {};
if (document.cookie.match(/^\s*\$Version=(?:"1"|1);\s*(.*)/)) {
c = RegExp.$1;
v = 1;
}
if (v === 0) {
c.split(/[,;]/).map(function(cookie) {
var parts = cookie.split(/=/, 2),
name = decodeURIComponent(parts[0].trimLeft()),
value = parts.length > 1 ? decodeURIComponent(parts[1].trimRight()) : null;
cookies[name] = value;
});
} else {
c.match(/(?:^|\s+)([!#$%&'*+\-.0-9A-Z^`a-z|~]+)=([!#$%&'*+\-.0-9A-Z^`a-z|~]*|"(?:[\x20-\x7E\x80\xFF]|\\[\x00-\x7F])*")(?=\s*[,;]|$)/g).map(function($0, $1) {
var name = $0,
value = $1.charAt(0) === '"'
? $1.substr(1, -1).replace(/\\(.)/g, "$1")
: $1;
cookies[name] = value;
});
}
return cookies;
}
function getCookie(name) {
return getCookies()[name];
}