Javascript操作cookie的函数代码
时间:2021-01-05 13:54:20|栏目:JavaScript代码|点击: 次
javascript操作cookie简单版本
function setCookie(name, value, iDay) {
var oDate = new Date();
oDate.setDate(oDate.getDate() + iDay);
document.cookie = name+'='+value+';expires='+oDate;
}
function getCookie(name) {
var arr = document.cookie.split('; ');
var i = 0;
for(i=0; i<arr.length; i++) {
var arr2 = arr[i].split('=');
if(arr2[0] == name) {return arr2[1];}
}
return '';
}
function removeCookie(name) {
setCookie(name,'',-1);
}
javascript操作cookie封装版本
var cookie=new function(){
this.set=function(name,value,hours){
var life=new Date().getTime();
life+=hours*1000*60;
var cookieStr=name+"="+escape(value)+";expires="+new Date(life).toGMTString();
document.cookie=cookieStr;
};
this.get=function(name){
var cookies = document.cookie.split(";");
if(cookies.length>0){
var cookie=cookies[0].split("=");
if(cookie[0]==name) {return unescape(cookie[1]);}
}
return null;
};
this.remove=function(name){
var cookieStr=name+"="+escape('null')+";expires="+new Date().toGMTString();
document.cookie=cookieStr;
};
}
留着备用吧
复制代码 代码如下:
function setCookie(name, value, iDay) {
var oDate = new Date();
oDate.setDate(oDate.getDate() + iDay);
document.cookie = name+'='+value+';expires='+oDate;
}
function getCookie(name) {
var arr = document.cookie.split('; ');
var i = 0;
for(i=0; i<arr.length; i++) {
var arr2 = arr[i].split('=');
if(arr2[0] == name) {return arr2[1];}
}
return '';
}
function removeCookie(name) {
setCookie(name,'',-1);
}
javascript操作cookie封装版本
复制代码 代码如下:
var cookie=new function(){
this.set=function(name,value,hours){
var life=new Date().getTime();
life+=hours*1000*60;
var cookieStr=name+"="+escape(value)+";expires="+new Date(life).toGMTString();
document.cookie=cookieStr;
};
this.get=function(name){
var cookies = document.cookie.split(";");
if(cookies.length>0){
var cookie=cookies[0].split("=");
if(cookie[0]==name) {return unescape(cookie[1]);}
}
return null;
};
this.remove=function(name){
var cookieStr=name+"="+escape('null')+";expires="+new Date().toGMTString();
document.cookie=cookieStr;
};
}
留着备用吧
上一篇:浅谈JavaScript 函数参数传递到底是值传递还是引用传递
栏 目:JavaScript代码
下一篇:JSON+JavaScript处理JSON的简单例子
本文地址:http://www.codeinn.net/misctech/41089.html