• JS操作cookies方法


    /**

    * 写入cookies
    */
    function setCookie(name, value) {
    var time = "1d"; //默认一天失效
    var strsec = getsec(time);
    var exp = new Date();
    exp.setTime(exp.getTime() + strsec * 1);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
    }

    /**
    * 读取cookies
    */
    function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg))
    return (arr[2]);
    else
    return null;
    }

    /**
    * 删除cookies
    */
    function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null)
    document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
    }

    /**
    * 写入cookie
    * 可自定义过期时间
    */
    function setCookie(name, value, time) {
    var strsec = getsec(time);
    var exp = new Date();
    exp.setTime(exp.getTime() + strsec * 1);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
    }

    /**
    * 获取时间
    * s:秒,如20秒为 s20;
    * h:小时,如12小时为 h12;
    * d:天,如30天为 d30
    * @param {[type]} str [description]
    * @return {[type]} [description]
    */
    function getsec(str) {
    var str1 = str.substring(1, str.length) * 1;
    var str2 = str.substring(0, 1);
    if (str2 == "s") {
    return str1 * 1000;
    } else if (str2 == "h") {
    return str1 * 60 * 60 * 1000;
    } else if (str2 == "d") {
    return str1 * 24 * 60 * 60 * 1000;
    }
    }

    // 实例
    setCookie("name","value","s20");

  • 相关阅读:
    Expedition---POJ
    LIS的优化算法O(n log n)
    Super Jumping! Jumping! Jumping! ---HDU
    数据库连接判断
    android stuido控件
    sql查询语句
    c# 字符串操作
    windows操作
    C# sql操作
    datagridview
  • 原文地址:https://www.cnblogs.com/huliang56/p/5707186.html
Copyright © 2020-2023  润新知