在javascript 怎么把变量写在cookie里面啊
发布网友
发布时间:2022-04-22 10:45
我来回答
共5个回答
热心网友
时间:2022-04-22 12:14
var cookieUtil={
get:function(name){
var cookieName=encodeURIComponent(name) + "=",
cookieStart=document.cookie.indexOf(cookieName),
cookieValue=null;
if(cookieStart>-1){
var cookieEnd=document.cookie.indexOf(';',cookieStart);
if(cookieEnd==-1){
cookieEnd=document.cookie.length;
}
cookieValue=decodeURIComponent(document.cookie.substring(cookieStart+cookieName.length,cookieEnd));
}
return cookieValue;
},
set:function(name,value,expires,path,domain,secure){
var cookieText=encodeURIComponent(name)+"="+encodeURIComponent(value);
if(expires instanceof Date){
cookieText +="; expires="+expires.toGMTString();
}
if(path){
cookieText +="; path="+ path;
}
if(domain){
cookieText +="; domain="+ domain;
}
if(secure){
cookieText +="; secure";
}
document.cookie= cookieText;
}
}
var cookieValue="myCookie";
window.onload=function(){
cookieUtil.set('myCookieName',cookieValue,new Date('January 1,2018'),"/");
}
javascript本身并没有直接读写Cookie的操作,上面的代码是我直接写了一个Cookie的类,可以读写Cookie。
热心网友
时间:2022-04-22 13:32
里面代码都是工作中积累的,可以多看看!谢谢
请参考
http://hi.baidu.com/smalltutu/blog/item/bfb68f54b9d993153a29351e.html
参考资料:http://hi.baidu.com/smalltutu/blog/item/bfb68f54b9d993153a29351e.html
热心网友
时间:2022-04-22 15:07
http://www.w3school.com.cn/js/js_cookies.asp
热心网友
时间:2022-04-22 16:58
//设置Cookie
function setCookies(varName,value)
{
var Days = 30; //此 cookie 将被保存 30 天
var exp = new Date();
exp.setTime(exp.getTime() + Days*24*60*60*1000);
var expiresDate = exp.toGMTString();
document.cookie = varName + "="+ value +";expire*="+ expiresDate;
}
热心网友
时间:2022-04-22 19:06
document.cookie = "test=" + escape(‘value’)
http://e.codepub.com/2009/0528/4789.php
这个教程不错看看就知道了
参考资料:http://e.codepub.com/2009/0528/4789.php