Cookie js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
var CookieManager = { set: function (name, value, days) { var expires = ""; if (days) { var d = new Date(); d.setTime(d.getTime() + (days * 24 * 60 * 60 * 1000)); expires = "; expires=" + d.toGMTString(); } document.cookie = name + "=" + value + expires + "; path=/"; return this.get(name); }, get: function (name) { name += "="; var b = document.cookie.split(';'), c; for (var i = 0; i < b.length; i++) { c = b[i].replace(/(^\s+)|(\s+$)/g, ""); while (c.charAt(0) == ' ') c = c.substring(1, c.length); if (c.indexOf(name) == 0) return c.substring(name.length, c.length); } return null; }, remove: function (name) { this.set(name, "", -1); } }; |
Пример использования
1 2 3 |
CookieManager.set('name', 'value'); // параметр days можно не указывать alert(CookieManager.get('name')); CookieManager.remove('name'); |