JavaScript的Cookies函数库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
//   -------------------------  
//   JavaScript的Cookies函数库
//   -------------------------  
//   保存Cookie  
function saveCookie(name, value, expires, path, domain, secure) {
    var strCookie = name + "=" + value;
    if (expires) {
        //   计算Cookie的期限,   参数为天数  
        var curTime = new Date();
        curTime.setTime(curTime.getTime() + expires * 24 * 60 * 60 * 1000);
        strCookie += ";   expires=" + curTime.toGMTString();
    }
    //   Cookie的路径  
    strCookie += (path) ? ";   path=" + path : "";
    //   Cookie的Domain  
    strCookie += (domain) ? ";   domain=" + domain : "";
    //   是否需要保密传送,为一个布尔值  
    strCookie += (secure) ? ";   secure" : "";
    document.cookie = strCookie;
}
 
 
//   使用名称参数取得Cookie值,   null表示Cookie不存在  
function getCookie(name) {
    var strCookies = document.cookie;
    var cookieName = name + "=";     //   Cookie名称  
    var valueBegin, valueEnd, value;
    //   寻找是否有此Cookie名称  
    valueBegin = strCookies.indexOf(cookieName);
    if (valueBegin == -1) return null;     //   没有此Cookie  
    //   取得值的结尾位置  
    valueEnd = strCookies.indexOf(";", valueBegin);
    if (valueEnd == -1)
        valueEnd = strCookies.length;     //   最後一个Cookie  
    //   取得Cookie值  
    value = strCookies.substring(valueBegin + cookieName.length, valueEnd);
    return value;
}
 
//   检查Cookie是否存在  
function checkCookieExist(name) {
    if (getCookie(name))
        return true;
    else
        return false;
}
 
//   删除Cookie  
function deleteCookie(name, path, domain) {
    var strCookie;
    //   检查Cookie是否存在  
    if (checkCookieExist(name)) {
        //   设置Cookie的期限为己过期  
        strCookie = name + "=";
        strCookie += (path) ? ";   path=" + path : "";
        strCookie += (domain) ? ";   domain=" + domain : "";
        strCookie += ";   expires=Thu,   01-Jan-70   00:00:01   GMT";
        document.cookie = strCookie;
    }
}

 

//
//   -------------------------   
//   JavaScript的Cookies函数库
//   -------------------------   
//   保存Cookie   
function saveCookie(name, value, expires, path, domain, secure) {
    var strCookie = name + "=" + value;
    if (expires) {
        //   计算Cookie的期限,   参数为天数   
        var curTime = new Date();
        curTime.setTime(curTime.getTime() + expires * 24 * 60 * 60 * 1000);
        strCookie += ";   expires=" + curTime.toGMTString();
    }
    //   Cookie的路径   
    strCookie += (path) ? ";   path=" + path : "";
    //   Cookie的Domain   
    strCookie += (domain) ? ";   domain=" + domain : "";
    //   是否需要保密传送,为一个布尔值   
    strCookie += (secure) ? ";   secure" : "";
    document.cookie = strCookie;
}


//   使用名称参数取得Cookie值,   null表示Cookie不存在   
function getCookie(name) {
    var strCookies = document.cookie;
    var cookieName = name + "=";     //   Cookie名称   
    var valueBegin, valueEnd, value;
    //   寻找是否有此Cookie名称   
    valueBegin = strCookies.indexOf(cookieName);
    if (valueBegin == -1) return null;     //   没有此Cookie   
    //   取得值的结尾位置   
    valueEnd = strCookies.indexOf(";", valueBegin);
    if (valueEnd == -1)
        valueEnd = strCookies.length;     //   最後一个Cookie   
    //   取得Cookie值   
    value = strCookies.substring(valueBegin + cookieName.length, valueEnd);
    return value;
}

//   检查Cookie是否存在   
function checkCookieExist(name) {
    if (getCookie(name))
        return true;
    else
        return false;
}

//   删除Cookie   
function deleteCookie(name, path, domain) {
    var strCookie;
    //   检查Cookie是否存在   
    if (checkCookieExist(name)) {
        //   设置Cookie的期限为己过期   
        strCookie = name + "=";
        strCookie += (path) ? ";   path=" + path : "";
        strCookie += (domain) ? ";   domain=" + domain : "";
        strCookie += ";   expires=Thu,   01-Jan-70   00:00:01   GMT";
        document.cookie = strCookie;
    }
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。