javascript cookie functions


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



Copy this code and paste it in your HTML
  1. function createCookie(name,value,days) {
  2. if (days) {
  3. var date = new Date();
  4. date.setTime(date.getTime()+(days*24*60*60*1000));
  5. var expires = "; expires="+date.toGMTString();
  6. }
  7. else expires = "";
  8. document.cookie = name+"="+value+expires+"; path=/";
  9. }
  10.  
  11. function readCookie(name) {
  12. var nameEQ = name + "=";
  13. var ca = document.cookie.split(';');
  14. for(var i=0;i < ca.length;i++) {
  15. var c = ca[i];
  16. while (c.charAt(0)==' ') c = c.substring(1,c.length);
  17. if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  18. }
  19. return null;
  20. }
  21.  
  22. /* Example use
  23. window.onload = function(e) {
  24. var cookie = readCookie("style");
  25. var title = cookie ? cookie : getPreferredStyleSheet();
  26. setActiveStyleSheet(title);
  27. }
  28.  
  29. window.onunload = function(e) {
  30. var title = getActiveStyleSheet();
  31. createCookie("style", title, 365);
  32. }
  33. */

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.