How to set a cookie to expire in 1 hour in Javascript? With this script

437 Views Asked by At

Here's the code:

!function(e) {
  var n = !1;
  if ("function" == typeof define && define.amd && (define(e), n = !0), "object" == typeof exports && (module.exports = e(), n = !0), !n) {
    var o = window.Cookies,
      t = window.Cookies = e();
    t.noConflict = function() {
      return window.Cookies = o,
      t
    }
  }
}(function() {
  function e() {
    for (var e = 0, n = {}; e < arguments.length; e++) {
      var o = arguments[e];
      for (var t in o) 
        n[t] = o[t]
    }
    return n
  }
  function n(o) {
    function t(n, r, i) {
      var c;
      if ("undefined" != typeof document) {
        if (arguments.length > 1) {
          if (i = e({
            path: "/"
          }, t.defaults, i), "number" == typeof i.expires) {
            var a = new Date;
            a.setMilliseconds(a.getMilliseconds() + 864e5 * i.expires),
            i.expires = a
          }
          i.expires = i.expires
            ? i.expires.toUTCString()
            : "";
          try {
            c = JSON.stringify(r),
            /^[\{\[]/.test(c) && (r = c)
          } catch (e) {}
          r = o.write
            ? o.write(r, n)
            : encodeURIComponent(String(r)).replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent),
          n = encodeURIComponent(String(n)),
          n = n.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent),
          n = n.replace(/[\(\)]/g, escape);
          var f = "";
          for (var s in i) 
            i[s] && (f += "; " + s, i[s] !== !0 && (f += "=" + i[s]));
          return document.cookie = n + "=" + r + f
        }
        n || (c = {});
        for (
          var p = document.cookie
          ? document.cookie.split("; ")
          : [],
        d = 0; d < p.length; d++) {
          var u = p[d].split("="),
            l = u.slice(1).join("=");
          '"' === l.charAt(0) && (l = l.slice(1, -1));
          try {
            var g = u[0].replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent);
            if (
              l = o.read
              ? o.read(l, g)
              : o(l, g) || l.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent),
            this.json) 
              try {
                l = JSON.parse(l)
              } catch (e) {}
            if (n === g) {
              c = l;
              break
            }
            n || (c[g] = l)
          } catch (e) {}
        }
        return c
      }
    }
    return t.set = t,
    t.get = function(e) {
      return t.call(t, e)
    },
    t.getJSON = function() {
      return t.apply({
        json: !0
      }, [].slice.call(arguments))
    },
    t.defaults = {},
    t.remove = function(n, o) {
      t(n, "", e(o, {expires: -1}))
    },
    t.withConverter = n,
    t
  }
  return n(function() {})
});

1

There are 1 best solutions below

1
On

Your code snippet add a Cookie property to the global window object. This property has a method set to add a Cookie, that takes the cookie name, content and properties as arguments. In the third argument you can define an expires properties and thus specify the duration of the cookie in milliseconds. Here is an example:

!function(e) {
  var n = !1;
  if ("function" == typeof define && define.amd && (define(e), n = !0), "object" == typeof exports && (module.exports = e(), n = !0), !n) {
    var o = window.Cookies,
      t = window.Cookies = e();
    t.noConflict = function() {
      return window.Cookies = o,
      t
    }
  }
}(function() {
  function e() {
    for (var e = 0, n = {}; e < arguments.length; e++) {
      var o = arguments[e];
      for (var t in o) 
        n[t] = o[t]
    }
    return n
  }
  function n(o) {
    function t(n, r, i) {
      var c;
      if ("undefined" != typeof document) {
        if (arguments.length > 1) {
          if (i = e({
            path: "/"
          }, t.defaults, i), "number" == typeof i.expires) {
            var a = new Date;
            a.setMilliseconds(a.getMilliseconds() + 864e5 * i.expires),
            i.expires = a
          }
          i.expires = i.expires
            ? i.expires.toUTCString()
            : "";
          try {
            c = JSON.stringify(r),
            /^[\{\[]/.test(c) && (r = c)
          } catch (e) {}
          r = o.write
            ? o.write(r, n)
            : encodeURIComponent(String(r)).replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent),
          n = encodeURIComponent(String(n)),
          n = n.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent),
          n = n.replace(/[\(\)]/g, escape);
          var f = "";
          for (var s in i) 
            i[s] && (f += "; " + s, i[s] !== !0 && (f += "=" + i[s]));
          return document.cookie = n + "=" + r + f
        }
        n || (c = {});
        for (
          var p = document.cookie
          ? document.cookie.split("; ")
          : [],
        d = 0; d < p.length; d++) {
          var u = p[d].split("="),
            l = u.slice(1).join("=");
          '"' === l.charAt(0) && (l = l.slice(1, -1));
          try {
            var g = u[0].replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent);
            if (
              l = o.read
              ? o.read(l, g)
              : o(l, g) || l.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent),
            this.json) 
              try {
                l = JSON.parse(l)
              } catch (e) {}
            if (n === g) {
              c = l;
              break
            }
            n || (c[g] = l)
          } catch (e) {}
        }
        return c
      }
    }
    return t.set = t,
    t.get = function(e) {
      return t.call(t, e)
    },
    t.getJSON = function() {
      return t.apply({
        json: !0
      }, [].slice.call(arguments))
    },
    t.defaults = {},
    t.remove = function(n, o) {
      t(n, "", e(o, {expires: -1}))
    },
    t.withConverter = n,
    t
  }
  return n(function() {})
});

// Use the set method, e.g.

window.Cookies.set('CookieName', 'CookieContent', { expires: 1000 * 60 * 60 })