js 패키지 쿠키 작업 의 함수 코드

1354 단어 포장 하 다cookie
핵심 코드:
 
var cookie={
setCookie:function (name, value, iDay) {
var oDate = new Date();
oDate.setDate(oDate.getDate()+iDay);
document.cookie = name+'='+value+';expires='+oDate;
},
getCookie:function (name) {
var arr = document.cookie.split('; ');
for (var i = arr.length - 1; i >= 0; i--) {
var arr2 = arr[i].split('=');
if(arr2[0]===name){
return arr2[1];
}
}
return '';
},
removeCookie:function (name) {
cookie.setCookie(name,1,-1);
}
};
테스트:<script>var cookie={setCookie:function(name,value,iDay){var oDate=new Date();oDate.setDate(oDate.getDate()+iDay); document.cookie = name+'='+value+';expires='+oDate; }, getCookie:function (name) { var arr = document.cookie.split('; '); for (var i = arr.length - 1; i >= 0; i--) { var arr2 = arr[i].split('='); if(arr2[0]===name){ return arr2[1]; } } return ''; }, removeCookie:function (name) { cookie.setCookie(name,1,-1); } }; cookie.setCookie('username','iceshu',33); // setCookie('pwd','ff',365); cookie.removeCookie('username'); alert(document.cookie) alert(cookie.getCookie('pwd')); </script>[Ctrl+A 선택:]

좋은 웹페이지 즐겨찾기