Simple JavaScript Cookie Functions


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /* Cookie Functions */
  2. function createCookie(name,value,days) {
  3. if (days) {
  4. var date = new Date();
  5. date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
  6. var expires = "; expires=" + date.toGMTString();
  7. }
  8. else {
  9. var expires = "";
  10. }
  11. document.cookie = name+"="+value+expires+"; path=/";
  12. }
  13.  
  14. function readCookie(name) {
  15. var nameEQ = name + "=";
  16. var ca = document.cookie.split(';');
  17. for(var i=0;i < ca.length;i++) {
  18. var c = ca[i];
  19. while (c.charAt(0)===' '){ c = c.substring(1,c.length); }
  20. if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  21. }
  22. return null;
  23. }
  24.  
  25. function eraseCookie(name) {
  26. createCookie(name,"",-1);
  27. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.