cookie操作实例


// cookie操作函数
var yearhisArray = new Array();
function init_cookies_array(yearhisArray_temp)
{
for( var i=0;i<10;i++)
{
var year_temp_cookies = getCookie(yearhisArray_temp+i);
yearhisArray.unshift(year_temp_cookies);
}
}
//将历史年份输入加入list
function change_yearList_Cookie( yearhisArray_temp){
for( var i=0;i {
if (yearhisArray[i]==document.getElementById("yearList").value)
{
return false ;
}
}
yearhisArray.unshift(document.getElementById("yearList").value);
yearhisArray.pop();
for(var i =0 ;i<10;i++){
addCookie(yearhisArray_temp+i,yearhisArray[i],30);
}
}
//cookie工具
function addCookie(objName,objValue,objHours)
{
var str = objName + "=" + escape(objValue);
if(objHours > 0){ //等于0时,关闭浏览器自动清除Cookies.
var date = new Date();
var ms = objHours*24*3600*1000;
date.setTime(date.getTime() + ms);
str += "; expires=" + date.toGMTString();
}
document.cookie = str;
}
function setCookie(name,value){//两个参数,一个是cookie的名子,一个是值
var Days = 30; //此 cookie 将被保存 30 天
var exp = new Date(); //new Date("December 31, 9998");
exp.setTime(exp.getTime() + 20*60*1000);
document.cookie = name + "="+ escape (value);
}
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();
}
}

你可能感兴趣的:(cookie操作实例)