一、创建cookie
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| function setCookie(name, value, expires, path, domain, secure) { var cookieText = encodeURIComponent(name) + '=' + encodeURIComponent(value); if (expires instanceof Date) { cookieText += '; expires=' + expires; } if (path) { cookieText += '; path=' + path; } if (domain) { cookieText += '; domain=' + domain; } if (secure) { cookieText += '; secure='+secure; } document.cookie = cookieText; }
|
二、获取cookie
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| function getCookie(name) { var cookieName = encodeURIComponent(name) + '='; var cookieStart = document.cookie.indexOf(cookieName); var cookieValue = null;
if (cookieStart > -1) { var cookieEnd = document.cookie.indexOf(';', cookieStart); if (cookieEnd == -1) { cookieEnd = document.cookie.length; } cookieValue = decodeURIComponent( document.cookie.substring(cookieStart + cookieName.length, cookieEnd)); } return cookieValue; }
|
三、删除cookie
1 2 3
| function unsetCookie(name) { document.cookie = name + "= ; expires=" + new Date(0); }
|
四、失效天数,直接传一个天数即可
1 2 3 4 5 6 7 8 9
| function setCookieDate(day) { if (typeof day == 'number' && day > 0) { var date = new Date(); date.setDate(date.getDate() + day); } else { throw new Error('传递的day必须是一个天数,必须比0大'); } return date; }
|
五、HTML5中提供了localStorage方法
1 2 3 4 5 6 7 8 9 10 11
| localStorage.setItem('name', '李炎恢'); alert(localStorage.getItem('name'));
localStorage.book = '李炎恢'; alert(localStorage.book);
localStorage.removeItem('name');
|
PS:是永久保存的,保存在缓存里,只有手工删除或者清理浏览器缓存方可失效。在容量上也有一些限制,主要看浏览器的差异,Firefox3+、IE8+、Opera为5M,,Chrome和Safari为2.5M。