当前位置:编程学习 > JS >>

js /jquery 获取cookie值代码

js /jquery 获取cookie值代码
*/

function getcookie(name, dc){  
    var cname = name + "=";
    if (dc.length > 0){  
        var begin = dc.indexof(cname);
        if (begin != -1){  
            begin += cname.length;  
            end = dc.indexof(",", begin);  
            if (end == -1){  
                end = dc.length;  
            }  
            return dc.substring(begin, end);  
        }  
    }  
    return null;  
}

//jquery取值比较简单,因为他有个自带函数

jquery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toutcstring)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new date();
                date.settime(date.gettime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toutcstring();
    // use expires attribute, max-age is not supported by ie
        }
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeuricomponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookievalue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jquery.trim(cookies[i]);
                // does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookievalue = decodeuricomponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookievalue;
    }
};

//使用方法

$.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'zzzyk.com', secure: true });

补充:网页制作,js教程 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,