首页 > 代码库 > 【学习笔记】JavaScript设置与获取Cookie

【学习笔记】JavaScript设置与获取Cookie

获取Cookie:

 1 function getCookie(cookiename) {
 2     var result;
 3     var mycookie = document.cookie;
 4     var start2 = mycookie.indexOf(cookiename + "=");
 5     if (start2 > -1) {
 6         start = mycookie.indexOf("=", start2) + 1;
 7         var end = mycookie.indexOf(";", start);
 8 
 9         if (end == -1) {
10             end = mycookie.length;
11         }
12 
13         result = endecodeURIComponent(mycookie.substring(start, end));
14     }
15 
16     return result;
17 }

 

设置Cookie:

function setCookie(c_name,value,expiredays){
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +decodeURIComponent(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

 

【学习笔记】JavaScript设置与获取Cookie