zoukankan      html  css  js  c++  java
  • localStorage的增删查改封装函数

    (function (win,doc) {
    var GD16 = {
    /**
    * 创建localStorage
    * @param argument json || string
    */
    Items: function (argument) {
    if (typeof argument == 'object') {//创建
    for (var i in argument) {
    win.localStorage.setItem(i, argument[i]);
    }
    }
    else if (typeof argument == 'string') {//获取
    return win.localStorage.getItem(argument);
    }
    else {
    return console.error('参数只能是json|string');
    }
    },
    /**
    * 删除localStorage
    * @param val string || null
    */
    rmItem: function (val) {
    if (val) {
    win.localStorage.removeItem(val);
    } else {
    var json = window.localStorage;
    for (var i in json) {
    win.localStorage.removeItem(i);
    }
    }
    },
    /**
    * 设置cookie|删除cookie|修改cookie
    */
    setCookie: function (name, value, time) {
    var d = new Date(new Date().getTime() + time * 1000).toGMTString();
    document.cookie = '' + name + '=' + value + ';expires=' + d;
    },
    /**
    * 获取cookie
    * @param name
    * @returns {string}
    */
    getCookie: function (name) {
    var arr = document.cookie.split('; ');
    var value = '';
    for (var i = 0, len = arr.length; i < len; i++) {
    if (arr[i].split('=')[0] == name) {
    value = arr[i].split('=')[1];
    break;
    }
    }
    return value;
    }
    };
    win.GD16 = GD16;
    })(window,document);
  • 相关阅读:
    缺陷笔记
    Eclipse中常用快捷键
    PL/SQL中复制粘贴表结构信息
    request之setAtrribute
    list+map
    套接字初始化失败问题
    上机编程题(2016校招)
    动态规划
    VS中碰到的问题
    IE8添加元素报错《没有权限》错误
  • 原文地址:https://www.cnblogs.com/deng-00/p/6928658.html
Copyright © 2011-2022 走看看