有的时候我们想把某个值存储起来,不想让这个值在刷新的时候被重置。有两种方法,一种是把值存到数据库里面去,一种是把值存到cookie里面。
有的时候,为了方便,我们可以把值直接存到cookie里面。
下面是操作cookie的几个方法:
<script>
/***
*读取指定的Cookie值 readCookie("id");
*@param {string} cookieName Cookie名称
*/
function readCookie(cookieName) {
var theCookie = "" + document.cookie;
var ind = theCookie.indexOf(cookieName);
if(ind==-1 || cookieName=="") return "";
var ind1 = theCookie.indexOf(';',ind);
if(ind1==-1) ind1 = theCookie.length;
/*读取Cookie值*/
return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}
/***
* 设置Cookie值 setCookie("id",1);
* @param {string} cookieName Cookie名称
* @param {string} cookieValue Cookie值
* @param {number} expireTime Cookie过期时间,单位毫秒,1秒=1000毫秒,1分钟等于60000毫秒,1小时等于3600000毫秒,1天等于86400000毫秒
*/
function setCookie(cookieName, cookieValue,expireTime) {
expireTime = expireTime||600000; //默认过期时间10分钟
/*当前日期*/
var today = new Date();
/*Cookie过期时间*/
var expire = new Date();
/*计算Cookie过期时间,1秒=1000毫秒,1分钟等于60000毫秒,1小时等于3600000毫秒,1天等于86400000毫秒*/
expire.setTime(today.getTime() + expireTime);
document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString();
}
/***
* 删除cookie中指定变量函数
* @param {string} $name Cookie名称
*/
function deleteCookie($name){
var myDate=new Date();
myDate.setTime(-1000);//设置时间
document.cookie=$name+"=''; expires="+myDate.toGMTString();
}
/***
* 删除cookie中所有定变量函数
* @param {string} cookieName Cookie名称
* @param {string} cookieValue Cookie值
* @param {number} nDays Cookie过期天数
*/
function clearCookie(){
var myDate=new Date();
myDate.setTime(-1000);//设置时间
var data=document.cookie;
var dataArray=data.split("; ");
for(var i=0;i<dataArray.length;i++){
var varName=dataArray[i].split("=");
document.cookie=varName[0]+"=''; expires="+myDate.toGMTString();
}
}
</script>
/***
*读取指定的Cookie值 readCookie("id");
*@param {string} cookieName Cookie名称
*/
function readCookie(cookieName) {
var theCookie = "" + document.cookie;
var ind = theCookie.indexOf(cookieName);
if(ind==-1 || cookieName=="") return "";
var ind1 = theCookie.indexOf(';',ind);
if(ind1==-1) ind1 = theCookie.length;
/*读取Cookie值*/
return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}
/***
* 设置Cookie值 setCookie("id",1);
* @param {string} cookieName Cookie名称
* @param {string} cookieValue Cookie值
* @param {number} expireTime Cookie过期时间,单位毫秒,1秒=1000毫秒,1分钟等于60000毫秒,1小时等于3600000毫秒,1天等于86400000毫秒
*/
function setCookie(cookieName, cookieValue,expireTime) {
expireTime = expireTime||600000; //默认过期时间10分钟
/*当前日期*/
var today = new Date();
/*Cookie过期时间*/
var expire = new Date();
/*计算Cookie过期时间,1秒=1000毫秒,1分钟等于60000毫秒,1小时等于3600000毫秒,1天等于86400000毫秒*/
expire.setTime(today.getTime() + expireTime);
document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString();
}
/***
* 删除cookie中指定变量函数
* @param {string} $name Cookie名称
*/
function deleteCookie($name){
var myDate=new Date();
myDate.setTime(-1000);//设置时间
document.cookie=$name+"=''; expires="+myDate.toGMTString();
}
/***
* 删除cookie中所有定变量函数
* @param {string} cookieName Cookie名称
* @param {string} cookieValue Cookie值
* @param {number} nDays Cookie过期天数
*/
function clearCookie(){
var myDate=new Date();
myDate.setTime(-1000);//设置时间
var data=document.cookie;
var dataArray=data.split("; ");
for(var i=0;i<dataArray.length;i++){
var varName=dataArray[i].split("=");
document.cookie=varName[0]+"=''; expires="+myDate.toGMTString();
}
}
</script>