js cookie

   SetCookie: function (name, value)//设定Cookie值 
    {
        var expdate = new Date();
        var argv = arguments;
        var argc = 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;
        if (expires != null) expdate.setTime(expdate.getTime() + (expires * 1000));
        document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString()))
   + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain))
   + ((secure == true) ? "; secure" : "");
    },
    DelCookie: function (name)//删除Cookie 
    {
        var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        var cval = GetCookie(name);
        document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
    },
    GetCookie: function (cookieName) {
        var cookieString = document.cookie;
        var start = cookieString.indexOf(cookieName + '=');
        if (start == -1) return null; // 加上等号的原因是避免在某些 Cookie 的值里有与cookieName一样的字符串 
        start += cookieName.length + 1;
        var end = cookieString.indexOf(';', start);
        if (end == -1) return unescape(cookieString.substring(start));
        return unescape(cookieString.substring(start, end));
    }

posted @ 2013-11-20 18:00  (二少)在南极  阅读(194)  评论(0编辑  收藏  举报