DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> Javascript操作cookie的函數代碼
Javascript操作cookie的函數代碼
編輯:關於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;
};
}

留著備用吧
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved