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

Leech on 07/21/06


Tagged

cookies


Versions (?)


Who likes this?

3 people have marked this snippet as a favorite

panatlantica
jkochis
vali29


Cookie v1.0


Published in: JavaScript 


URL: http://jsfromhell.com/geral/cookie

Object to manage cookies. Created: 2006.04.24

  1. /**************************************
  2. * Jonas Raoni Soares Silva
  3. * http://www.joninhas.ath.cx
  4. **************************************/
  5.  
  6. Cookie = {
  7. isSupported: function(){
  8. var s = "{3F2504E0-4F89-11D3-9A0C-0305E82C3301}";
  9. return "cookie" in document && this.write(s, "OK") && this.read(s) == "OK" && this.remove(s);
  10. },
  11. exists: function(name){
  12. return document.cookie.indexOf(name + "=") + 1;
  13. },
  14. write: function(name, value, expires, path, domain, secure) {
  15. expires instanceof Date ? expires = expires.toGMTString()
  16. : typeof(expires) == 'number' && (expires = (new Date(+(new Date) + expires * 1e3)).toGMTString());
  17. var r = [name + "=" + escape(value)], s, i;
  18. for(i in s = {expires: expires, path: path, domain: domain})
  19. s[i] && r.push(i + "=" + s[i]);
  20. return secure && r.push("secure"), document.cookie = r.join(";"), true;
  21. },
  22. read: function(name){
  23. var c = document.cookie, s = this.exists(name), e;
  24. return s ? unescape(c.substring(s += name.length, (c.indexOf(";", s) + 1 || c.length + 1) - 1)) : "";
  25. },
  26. remove: function(name, path, domain){
  27. return this.exists(name) && this.write(name, "", new Date(0), path, domain);
  28. }
  29. };

Report this snippet 

You need to login to post a comment.