Advertisement
krot

setCookie

Oct 7th, 2017
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function setCookie(name, value, options) {
  2.   options = options || {};
  3.  
  4.   var expires = options.expires;
  5.  
  6.   if (typeof expires == "number" && expires) {
  7.     var d = new Date();
  8.     d.setTime(d.getTime() + expires * 1000);
  9.     expires = options.expires = d;
  10.   }
  11.   if (expires && expires.toUTCString) {
  12.     options.expires = expires.toUTCString();
  13.   }
  14.  
  15.   value = encodeURIComponent(value);
  16.  
  17.   var updatedCookie = name + "=" + value;
  18.  
  19.   for (var propName in options) {
  20.     updatedCookie += "; " + propName;
  21.     var propValue = options[propName];
  22.     if (propValue !== true) {
  23.       updatedCookie += "=" + propValue;
  24.     }
  25.   }
  26.  
  27.   document.cookie = updatedCookie;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement