当前位置 : 主页 > 网络编程 > JavaScript >

local_cookie_sessing_storage.js

来源:互联网 收集:自由互联 发布时间:2021-06-30
local_cookie_sessing_storage.js util.cookie = { set: function (name, value, options) { options = options || {}; if (value === null) { value = ''; options.expires = -1; } let expires = ''; if (options.expires (typeof options.expires === 'num
local_cookie_sessing_storage.js
util.cookie = {
  set: function (name, value, options) {
    options = options || {};

    if (value === null) {
      value = '';
      options.expires = -1;
    }

    let expires = '';

    if (options.expires && (typeof options.expires === 'number' || options.expires.toUTCString)) {
      let date;

      if (typeof options.expires === 'number') {
        date = new Date();
        date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
      } else {
        date = options.expires;
      }

      expires = ';expires=' + date.toUTCString();
    }

    let path = options.path ? ';path=' + options.path : '';
    let domain = options.domain ? ';domain=' + options.domain : '';
    let secure = options.secure ? ';secure' : '';

    document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('')
  },
  get: function (name, defaultVal) {
    let cookieValue = defaultVal || null;

    if (document.cookie && document.cookie !== '') {
      let cookies = document.cookie.split(';');

      for (let i = 0; i < cookies.length; i++) {
        let cookie = util.trim(cookies[i]);

        if (cookie.substring(0, name.length + 1) === (name + '=')) {
          cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
          break;
        }
      }
    }

    return cookieValue;
  }
}

util.session = {
  set: function (key, value) {
    sessionStorage.setItem(key, value)
  },
  get: function (key) {
    return sessionStorage.getItem(key)
  },
  setJson: function (key, obj) {
    sessionStorage.setItem(key, json_encode(obj))
  },
  getJson: function (key) {
    return json_decode(this.get(key))
  },
  key: function (index) {
    return sessionStorage.key(index)
  },
  has: function (key) {
    return this.get(key) !== null
  },
  len: function () {
    return sessionStorage.length
  },
  clear: function () {
    sessionStorage.clear()
  }
}

util.storage = {
  set: function (key, value) {
    localStorage.setItem(key, value)
  },
  get: function (key) {
    return localStorage.getItem(key)
  },
  sets: function (datas) {
    datas.forEach(data => this.set(data.key, data.value))
  },
  gets: function (keys) {
    return keys.map(key => this.get(key))
  },
  setJson: function (key, obj) {
    localStorage.setItem(key, json_encode(obj))
  },
  getJson: function (key) {
    return json_decode(this.get(key))
  },
  del: function (key) {
    return localStorage.removeItem(key)
  },
  key: function (index) {
    return localStorage.key(index)
  },
  has: function (key) {
    return this.get(key) !== null
  },
  len: function () {
    return localStorage.length
  },
  clear: function (keys) {
    if (keys) {
      keys.forEach(key => this.del(key))
    } else {
      localStorage.clear()
    }
  }
}
网友评论