cookie

原创 韩秀利 随笔 javascript 147阅读 2018-04-12 14:24:47 举报
function setCookie(cname,cvalue,exdays){
    var d = new Date();
    d.setTime(d.getTime()+(exdays*24*60*60*1000));
    var expires = "expires="+d.toGMTString();
    document.cookie = cname + "=" + cvalue + "; " + expires+"; path=/";
}
setCookie('name','hxl','30');

function getCookie(cname){
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) 
    {
        var c = ca[i].trim();
        if (c.indexOf(name)==0) return c.substring(name.length,c.length);
    }
    return "";
}
console.log(getCookie('name'));
评论 ( 0 )
最新评论
暂无评论

赶紧努力消灭 0 回复