|
JavaScript cookiesMethods for working with cookies from JavaScript: Example of use. Serializing a two-dimensional array in a cookie:function setCookie(name, value, expires, path, domain, secure) { if (!name || !value) return false; var str = name + '=' + encodeURIComponent(value); if (expires) str += '; expires=' + expires.toGMTString(); if (path) str += '; path=' + path; if (domain) str += '; domain=' + domain; if (secure) str += '; secure'; document.cookie = str; return true; } function getCookie(name) { var pattern = "(?:;)?" + name + "=([^;]*);?"; var regexp = new RegExp(pattern); if (regexp.test(document.cookie)) return decodeURIComponent(RegExp["$1"]); return false; } function deleteCookie(name, path, domain) { setCookie(name, null, new Date(0), path, domain); return true; } Reading cookies and deserializing the array://We get the current time and date. var d = new Date(); //We increase the date by 6 months. d.setMonth(d.getMonth() + 6); //Save current color array to cookie currentcolor for 6 months. setCookie('current_color', current_color.join(','), d); var color = getCookie('current_color'); if (color) { eval('current_color=[' + color + ']'); } |
About Cookies
Settings
Programming
Creating and reading cookie
|