Internet review - Lite Web-master Graphics Games Freeware Time Christmas Bible
Software Software online Browsers Smartphones

JavaScript cookies


Methods for working with cookies from JavaScript:

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;
}
Example of use. Serializing a two-dimensional array in a cookie:
//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);
Reading cookies and deserializing the array:
var color = getCookie('current_color');
if (color) {
    eval('current_color=[' + color + ']');
}

About Cookies Settings

Programming
Creating and reading cookie



Mobile version

Terms of publication of the article
Advertising
About us
Graphics

Fonts
Logos
Brandbooks
Pictogramms
Heraldry

Popular

Check a website level
A website registration
How to creat a website
#1 on Google
Online Translators
Password

Internet top

©2005-2024, Web studio Ph4 - Internet Catalog for user, web-master and designer v. 6.0.3