下面是我试图弄清楚为什么它无法正常工作的示例。
它一直给我空值。
我正在尝试学习如何在单个cookie中设置多个值。
每个Cookie包含一个“名称=值”对,因此,如果您需要存储几条单独的数据(例如用户名,年龄和成员(member)编号),则需要三个不同的Cookie。
而不是这样做,我使用分隔符技术来拆分它,以便可以使用更少的cookie。
如果可以的话,请向我显示正确实现此示例的任何示例。我已经看过其他编程语言的一些资料,但我想知道它是如何专门在JavaScript中完成的。
<!DOCTYPE html>
<html>
<head>
<title> Using a delimiter to save on cookies used</title>
<script>
function createCookie(name, value, days, path, domain, secure) {
if (days) {
var date=new Date();
date.setTime(date.getTime()+ (days*24*60*60*1000));
var expires=date.toGMTString();
}
else var expires = "";
cookieString= name + "=" + escape (value);
if (expires)
cookieString += "; expires=" + expires;
if (path)
cookieString += "; path=" + escape (path);
if (domain)
cookieString += "; domain=" + escape (domain);
if (secure)
cookieString += "; secure";
document.cookie=cookieString;
}
function getCookie(name) {
var nameEquals = name + "=";
var crumbs=document.cookie.split('|');
for (var i = 0; i<crumbs.length; i++) {
var crumb = crumbs [i];
if (crumb.indexOf(nameEquals) == 0) {
return unescape (crumb.substring(nameEquals.length, crumb.length));
}
}
return null;
}
function deleteCookie(name){
createCookie(name, "", -1);
}
var userdata="Joe|31|Athlete";
createCookie("user", userdata);
var myUser=getCookie("user");
var myUserArray=myUser.split('|');
var name=myUserArray[0];
var age=myUserArray[1];
var profession=myUserArray[2];
</script>
</head>
<body>
</body>
</html>
最佳答案
不要使用“|”作为分隔符,它在不同的浏览器中的实现方式有所不同(将“-”,“+”,“。”等用于分隔符)。当我检查firefox>网站信息时,它告诉我有一个cookie,但是内容是Joe%7C31%7CAthlete
。在我的版本中,我将定界符放在我们在所有内容之前定义的变量中(我取了'---',但您可以根据需要更改它):
<!DOCTYPE html>
<html>
<head>
<title> Using a delimiter to save on cookies used</title>
<script>
var delimiter = "---";
function createCookie(name, value, days, path, domain, secure) {
if (days) {
var date=new Date();
date.setTime(date.getTime()+ (days*24*60*60*1000));
var expires=date.toGMTString();
}
else var expires = "";
cookieString= name + "=" + escape (value);
if (expires)
cookieString += "; expires=" + expires;
if (path)
cookieString += "; path=" + escape (path);
if (domain)
cookieString += "; domain=" + escape (domain);
if (secure)
cookieString += "; secure";
document.cookie=cookieString;
}
function getCookie(name) {
var nameEquals = name + "=";
var whole_cookie=document.cookie.split(nameEquals)[1].split(";")[0]; // get only the value of the cookie we need
// (split by the name=, take everything after the name= and then split it by ";" and take everything before the ";")
var crumbs=whole_cookie.split(delimiter); // split now our cookie in its information parts
/*for (var i = 0; i<crumbs.length; i++) {
var crumb = crumbs [i];
if (crumb.indexOf(nameEquals) == 0) {
return unescape (crumb.substring(nameEquals.length, crumb.length));
}
}*/ // sorry... i dont understand what this does ;) but it works without it
return crumbs; // return the information parts as an array
}
function deleteCookie(name){
createCookie(name, "", -1);
}
// ---------------------
// DEBUGGING PART STARTS
// ---------------------
var userdata="Joe"+delimiter+"31"+delimiter+"Athlete";
createCookie("user", userdata);
var myUserArray=getCookie("user");
var name=myUserArray[0];
var age=myUserArray[1];
var profession=myUserArray[2];
alert(myUserArray[0]);
alert(myUserArray[1]);
alert(myUserArray[2]);
// ---------------------
// DEBUGGING PART ENDS
// ---------------------
</script>
</head>
<body>
</body>
</html>
当我对此进行测试时,它可以正常工作:它将“Joe”保存在
name
中,将“31”保存在age
中,并将“运动员”保存在job
中我添加了一些调试警报,以便您也可以对其进行测试
再见,我希望我能帮助:)
关于javascript - 如何使用定界符在单个Cookie中设置多个值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16779635/