zoukankan      html  css  js  c++  java
  • js cookie

    /**
     * This js provide a simple way to operate cookie in javascript
     * function getCookie(name);
     * function setCookie(name,value[,expireDays]);
     * function deleteCookie(name);
     */
    function getCookie(name)
    {
        var arg = name + "=";
        var alen = arg.length;
        var clen = document.cookie.length;
        var i = 0;
        while (i < clen)
        {
            var j = i + alen;
            if (document.cookie.substring(i, j) == arg)
                return getCookieVal(j);
            i = document.cookie.indexOf(" ", i) + 1;
            if (i == 0) break;
        }
        return "";
    }

    function setCookie(name, value)
    {
        var argv = setCookie.arguments;
        var argc = setCookie.arguments.length;
        var expDay = (argc > 2) ? argv[2] : -1;
        try
        {
            expDay = parseInt(expDay);
        }
        catch(e)
        {
            expDay = -1;
        }
        if(expDay < 0) {
            setCookieVal(name, value);
        } else {
            var expDate = new Date();
            // The expDate is the date when the cookie should expire, we will keep it for a month
            expDate.setTime(expDate.getTime() + (expDay * 24 * 60 * 60 * 1000));
            setCookieVal(name, value, expDate);
        }
    }

    function deleteCookie(name)
    {
        var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        // This cookie is history
        var cval = getCookie(name);
        document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
    }

    function getCookieVal(offset)
    {
        var endstr = document.cookie.indexOf(";", offset);
        if (endstr == -1)
            endstr = document.cookie.length;
        return unescape(document.cookie.substring(offset, endstr));
    }

    function setCookieVal(name, value)
    {
        var argv = setCookieVal.arguments;
        var argc = setCookieVal.arguments.length;
        var expires = (argc > 2) ? argv[2] : null;
        var path = (argc > 3) ? argv[3] : null;
        var domain = (argc > 4) ? argv[4] : null;
        var secure = (argc > 5) ? argv[5] : false;
        document.cookie = name + "=" + escape(value) +
                          ((expires == null || expires < 0) ? "" : ("; expires=" + expires.toGMTString())) +
                          ((path == null) ? "" : ("; path=" + path)) +
                          ((domain == null) ? "" : ("; domain=" + domain)) +
                          ((secure == true) ? "; secure" : "");
    }

  • 相关阅读:
    Linux文件权限详解
    linux软链接和硬链接的区别
    linux vi编辑常用命令
    juery下拉刷新,div加载更多元素并添加点击事件(二)
    性能调优常见问题与方案
    测试人员怎么避免背黑锅?
    测试部工作检查观点
    如何为一组任务确定计划,估计每个任务所需的时间?
    测试人员和开发人员如何更高效的配合工作
    测试人员职业规划
  • 原文地址:https://www.cnblogs.com/tangself/p/1623558.html
Copyright © 2011-2022 走看看