Posted By

noah on 06/11/07


Tagged

date browser save cookies id state identification tracking robzand utilities surveillance


Versions (?)

Who likes this?

7 people have marked this snippet as a favorite

luman
vali29
nrambeck
loric
guo3823538
onelittlerobot
Beppoi


get and set cookie


 / Published in: JavaScript
 

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

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: nrambeck on December 12, 2008

The regex for grabbing cookie data saved me about 10 lines of code.

You need to login to post a comment.