Posted By

noah on 05/22/07


Tagged

browser cookies web state ui interactive ixd tracking metrics


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

vali29


Get and set state with cookie II


 / Published in: JavaScript
 

Set and get a cookie.

  1. function cookieTime(days){
  2. var now = new Date();
  3. var exp = new Date();
  4. var x = Date.parse(now) + days*24*60*60*1000;
  5. exp.setTime(x);
  6. str = exp.toUTCString();
  7. re = '/(\d\d)\s(\w\w\w)\s\d\d(\d\d))/';
  8. return str.replace(re,"$1-$2-$3");
  9. }
  10.  
  11. function setCookie ( id, value) {
  12. document.cookie = id+'='+value+';path=/;expires='+cookieTime(365);
  13. }
  14.  
  15. function getCookie ( id, defaultValue ) {
  16. var re = new RegExp(id+'=(.*)');
  17. var value = re.exec(document.cookie);
  18. return (value) ? value[1].split(';')[0] : defaultValue;
  19. }

Report this snippet  

You need to login to post a comment.