js写cookie,如何完成其保存功能
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js写cookie,如何完成其保存功能相关的知识,希望对你有一定的参考价值。
在进入一个页面后,还没登陆之前,如何在用户的cookie文件中存放一个SessionId。由于第一次用JS写,以前对这方面不是很了解,原先写了一个,结果是登陆之后的。(比如说我要对某个东西顶或踩,但并不需要我登陆才能完成此操作) function setCookie(name)
var Days = 15;
var exp=new Date();
exp.setTime(exp.getTime()+Days*24*3600*1000);
var value=Math.floor(Math.random()*100000000);
var name=name+"="+escape(value);
document.cookie=name+";expires"+exp.toGMTString();
alert(document.cookie);
//获得用户的cookie并检测
function checkCookie(name)
var strcookie=document.cookie;
var name=eywaId+"=";
if(name=="")
setCookie(eywaId);
var arrcookie=strcookie.split(";");//获得cookie中所有的元素
for(var i=0;i<arrcookie.length;i++)//遍历arrcookie
var arr=arrcookie[i].split("=");
if(arr[0]==eywaId)
return unescape(arr[1]);
return name;
这是自己尝试写的,我要怎样才能把eywaId存到用户的cookie文件中
//获得Cookie解码后的值
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
//---------------------------
function SetCookie(name, value)
//设定Cookie值
var expdate = new Date();
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
if(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ));
document.cookie = name + "=" + escape (value) +((expires == null) ? "" : ("; expires="+ expdate.toGMTString()))
+((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))
+((secure == true) ? "; secure" : "");
//---------------------------------
function DelCookie(name)
//删除Cookie
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires="+ exp.toGMTString();
//------------------------------------
function GetCookie(name)
//获得Cookie的原始值
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen)
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return GetCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
return null;
SetCookie("username1",99);
alert(GetCookie("username1"));本回答被提问者采纳 参考技术B 网上找一个
js操作cookie
经常在网页上看到浏览记录,在没有登陆的情况下,是不太可能把数据保存在数据库中的,那多半是放在cookie 中的,不知道这些怎么去操作,大哥们写点代码让小弟学习下。
也可发到我的邮箱:zhangxiaohui.cn@163.com
<script language="javascript"src="./js/cookie.js"></script>
//取cookie值
var hrckdeptcode = Cookie("hrckdeptcode");
var hrckdeptname = Cookie("hrckdeptname");
//保存cookie值
setCookie("hrckdeptcode", deptcode, expdate, "/", null, false);
setCookie("hrckdeptname", deptname, expdate, "/", null, false);
把下面的脚本放到 cookie.js 文件里
var expdate= new Date();
function getCookieVal (offset)
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1) endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
function Cookie (name)
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen)
var j = i + alen;
if (document.cookie.substring(i, j) == arg) return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
return null;
function setCookie (name,value,expires,path,domain,secure)
expdate.setTime(expdate.getTime() + (24*60*60*1000*365));
document.cookie = name + "=" + escape (value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
function deleteCookie(name)
expdate = new Date();
expdate.setTime(expdate.getTime() - (86400 * 1000 * 1));
setCookie(name, "", expdate);
本回答被提问者采纳
以上是关于js写cookie,如何完成其保存功能的主要内容,如果未能解决你的问题,请参考以下文章