我想在Cookie中保存数字的值
var num = 1;
我使用这两个函数来设置cookie并阅读它
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
};
和
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0)
var x = c.substring(nameEQ.length,c.length);
console.info("Value of num = " +x);
}
};
我像这样调用函数
cname = "cookie1";
createCookie(cname,num,0);
readCookie(cname);
在控制台中,我得到cookie未定义或类似“5_655666 = xKV1REGZllELnKC1ZJQ7BAlbbFYAAAAA4WCdPor9VSiD08vABGt6oA ==” “cookie1 gers = false” “cookie1 nds = false” 很多时候我无法弄清楚问题是什么。