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" : "");
    }

  • 相关阅读:
    备份
    >> 与 > >
    为什么需要htons(), ntohl(), ntohs(),htons() 函数
    小技巧
    C++头文件
    宏定义中的#,##操作符和... and _ _VA_ARGS_ _与自定义调试信息的输出
    OpenCV摄像头简单程序
    [转]让Linux的tty界面支持中文
    opencv 2 computer vision application programming第四章翻译
    OpenCV条码(6)简单实现
  • 原文地址:https://www.cnblogs.com/tangself/p/1623558.html
Copyright © 2011-2022 走看看