js9:设置cookie,读取cookie,删除cookie,保存cookie时间,String,Date对象

原文发布时间为:2008-11-11 —— 来源于本人的百度文章 [由搬家工具导入]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>js</title>
<script type="text/javascript">
function setCookie(){
var url="js9.html?";
var time=new Date();
var savetime=document.getElementById("t").value;
if(isNaN(savetime)){   
alert('时间请输入数字');
return false;
}
time.setMinutes(time.getMinutes()+savetime);
document.cookie=encodeURI("txt1="+document.getElementById("txt1").value)+";expires="+time.toUTCString();
document.cookie=encodeURI("txt2="+document.getElementById("txt2").value)+";expires="+time.toUTCString();
location.href("js9.html");
}
function showdate(){
var dt=new Date();
//2008-06-09 21:23:16
dt.setDate(9);
dt.setMonth(6-1);
dt.setFullYear(2008);
dt.setHours(21);
dt.setMinutes(23);
dt.setSeconds(16);
// dt.setSeconds(10000);总时间
alert(dt.getDate()+","+dt.getDay()+","+dt.getFullYear()+","+dt.getHours()+","+dt.getMinutes()+","+dt.getMonth()+","+dt.getSeconds()+","+dt.getTime()+","+dt.toUTCString()+","+dt.toLocaleString());
}
function readCookie(){
var getdata=decodeURI(document.cookie);
alert(getdata);//给你看看cookie中保存的样子
if(getdata!="nextalert=yes"){
var datas=getdata.split("; ");//分后后面有一个空格
var cur;
for(var count=0;count<datas.length-1;count++){
cur=datas[count];
if(cur.substring(0,cur.indexOf("="))=="txt1")
var t1=cur.substring(cur.indexOf("=")+1,cur.length)
if(cur.substring(0,cur.indexOf("="))=="txt2")
var t2=cur.substring(cur.indexOf("=")+1,cur.length)
document.getElementById("txt1").value=t1;
document.getElementById("txt2").value=t2;
}
}
}
</script>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
<label>
<input name="txt2" type="text" id="txt2" />
</label>
<label>
<input name="txt1" type="text" id="txt1" />
</label>
<p>
<label>
<input name="t" type="text" id="t" />
</label>
(前面填入要保存多少分钟,输入数字,为负数时是删除cookie)</p>
<p>
<label>
<input name="save" type="button" id="save" onclick="setCookie();" value="保存cookie" />
<input name="read" type="button" id="read" value="读取cookie" onclick="readCookie();" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </label>
<label>
<input name="dt" type="button" id="dt" value="显示时间" onclick="showdate();" />
</label>
</p>
</form>
</body>
</html>

======================

 <script type="text/javascript">
//写cookies函数 作者:翟振凯
function SetCookie(name,value)//两个参数,一个是cookie的名子,一个是值
{
var Days = 30; //此 cookie 将被保存 30 天
var exp  = new Date();    //new Date("December 31, 9998");
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getCookie(name)//取cookies函数      
{
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null) return unescape(arr[2]); return null;

}
function delCookie(name)//删除cookie
{
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval=getCookie(name);
if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}

SetCookie("url","http://hi.baidu.com/handboy");
alert(getCookie("url"));
//delCookie("url");

</script>

==========================

javascript cookie保存 json

http://lab.distilldesign.com/json-cookie/jquery.jsoncookie.js

http://www.json.org/json2.js

http://plugins.jquery.com/

在jsonCookie 对象中,有两个属性:jsonName和jsonNum。

  • jsonName定义json对象的名字;
  • jsonNum定义json对象中存储的个数。

另外有五个方法。

  • store(id,name,address) 该函数将数据存储到cookie中,接受三个参数。当然,你可以自己根据需要来增加参数的数量。
  • remove(id) 该函数用来删除数据,接受一个参数。要删除对象的id;
  • modify(id,key,value) 该函数修改已存对象的属性值,接受三个参数。一个id和相应的要修改对象的名字和新的属性值;
  • getJson() 该函数获得cookie中的json对象;
  • getNum() 该函数取得cookie中存储的json对象中存储的数据长度。

查看demo

原文地址:https://www.cnblogs.com/handboy/p/7148462.html