建站软件 优化软件 编程软件 网页辅助 站群程序 网站程序 图像处理 资源教程 字体下载 推荐软件


您的位置:首页 > javascript学习 > 怎么用js设置cookie

怎么用js设置cookie

时间:2015-09-20 13:46:53  来源:免费模板网 作者:风雪 阅读次数 tagscookie

在用户登录  注册的项目里面经常要用到cookie,来看用户是否已经登录,同样的原理,那么javascript怎么设置cookie呢

<html>
 
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>用js设置cookie,并能读取</title>
 
</head>
 
<body>
 
<!--script>
function getCookie(str){
var tmp,reg=new RegExp("(^| )"+str+"=([^;]*)(;|$)","gi");
if(tmp=reg.exec(document.cookie))return(tmp[2]);
return null;
}
var arr=["a","b","c"]
var arr2=["d","e","f"]
document.cookie="username=" + arr;
document.cookie="password=" + arr2;
if(arr=getCookie("username").split(","))alert(arr[1]);
if(arr=getCookie("password").split(","))alert(arr[1]);
</script-->
<script>
var NO=4;
var ArrayStr=new Array();for(i=0;i<NO;i++)ArrayStr[i]="null";
function AddArray(str,gor){
ArrayStr[gor-1]=str;
document.cookie="username="+ArrayStr;
}
function getCookie(str){
var tmp,reg=new RegExp("(^| )"+str+"=([^;]*)(;|$)","gi");
if(tmp=reg.exec(document.cookie))return(tmp[2]);
return null;
}
function ReadCookie(){
alert(getCookie("username").split(","));
}
</script>
<input type="button" onclick="AddArray(value,sing)" value="题目一" sing="1">
<input type="button" onclick="AddArray(value,sing)" value="题目二" sing="2">
<input type="button" onclick="AddArray(value,sing)" value="题目一" sing="3">
<input type="button" onclick="AddArray(value,sing)" value="题目二" sing="4">
<input type="button" value="查看答案" onclick="ReadCookie()">
 
</body>
 
</html>

本文地址:https://www.freemoban.com/javascript/2015/0920/2467.html

猜你喜欢
栏目推荐
模板推荐

Copyright:www.freemoban.com 免费模板网 All Rights Reserved 网站备案:辽ICP备19014872号-2   辽公网安备 21010602000376号  辽公网安备:42900402000182号

免责声明:本站部分资源来自互联网收集,版权归原创者所有,如果侵犯了你的权益,我们会及时删除侵权内容,联系QQ:1615187561 谢谢合作!