wei*_*8t1 5 javascript cookies
下面是我试图弄清楚为什么它不能正常工作的示例。
它一直给我空值。
我正在尝试学习如何在单个 cookie 中设置多个值。
每个 cookie 都包含一个名称=值对,因此如果您需要存储几条单独的数据,例如用户姓名、年龄和会员号,则需要三个不同的 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>
Run Code Online (Sandbox Code Playgroud)
不要使用“|” 作为分隔符,它在不同浏览器中的实现方式有所不同(使用“-”、“+”、“.”等作为分隔符)。当我检查 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>
Run Code Online (Sandbox Code Playgroud)
当我测试这个时,它工作正常:它保存了“Joe” name,“31”age和“Athlete”,job
我添加了一些调试警报,以便您也可以测试它
,再见,我希望我能提供帮助:)
| 归档时间: |
|
| 查看次数: |
15396 次 |
| 最近记录: |