Wednesday, June 4, 2014

set and get Cookies in javascripts




function setCookie(key, value) {
    var expires = new Date();
    expires.setTime(expires.getTime() + 31536000000); //1 year
    document.cookie = key + '=' + value + ';expires=' + expires.toUTCString();
}

function getCookie(key) {
    var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');
    return keyValue ? keyValue[2] : null;
}

  setCookie('Iwantto', '0');

getCookie('Iwantto');

No comments :

Post a Comment