We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

noah on 06/11/07


Tagged

date browser save cookies id state identification tracking robzand utilities surveillance


Versions (?)


Who likes this?

2 people have marked this snippet as a favorite

luman
vali29


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 

You need to login to post a comment.