2012年8月17日星期五

js 操作 cookies 学习

js 操作 cookies 学习

文章摘自 http://www.quirksmode.org/js/cookies.html

 1         function createCookie(name, value, days) { 2             if (days) { 3                 var date = new Date(); 4                 date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); 5                 var expires = "; expires=" + date.toGMTString(); 6             } 7             else var expires = ""; 8             document.cookie = name + "=" + value + expires + "; path=/"; 9         }10         function readCookie(name) {11             var nameEQ = name + "=";12             var ca = document.cookie.split(';');13             for (var i = 0; i < ca.length; i++) {14                 var c = ca[i];15                 while (c.charAt(0) == ' ') c = c.substring(1, c.length);16                 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);17             }18             return null;19         }20         function eraseCookie(name) {21             createCookie(name, "", -1);22         }

TAG: