码迷,mamicode.com
首页 > 其他好文 > 详细

cookie的存取删

时间:2017-10-24 14:54:21      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:log   else   char   function   cti   exp   ret   escape   etc   

存:
document.cookie = "name=Kevin;expires="+new Date().getDate()+7; //有效期7天
取:
function GetCookie(sName)
{
var aCookie = document.cookie.split("; ");
for (var i=0; i < aCookie.length; i++)
{
var aCrumb = aCookie[i].split("=");
if (sName == aCrumb[0])
return unescape(aCrumb[1]);
}
return null;
}
GetCookie(‘name‘)      //Kevin

 

//设置cookie  
function setCookie(cname, cvalue, exdays) {  
    var d = new Date();  
    d.setTime(d.getTime() + (exdays*24*60*60*1000));  
    var expires = "expires="+d.toUTCString();  
    document.cookie = cname + "=" + cvalue + "; " + expires;  
}  
//获取cookie  
function getCookie(cname) {  
    var name = cname + "=";  
    var ca = document.cookie.split(‘;‘);  
    for(var i=0; i<ca.length; i++) {  
        var c = ca[i];  
        while (c.charAt(0)==‘ ‘) c = c.substring(1);  
        if (c.indexOf(name) != -1) return c.substring(name.length, c.length);  
    }  
    return "";  
}  
//清除cookie    
function clearCookie(name) {    
    setCookie(name, "", -1);    
}    
function checkCookie() {  
    var user = getCookie("username");  
    if (user != "") {  
        alert("Welcome again " + user);  
    } else {  
        user = prompt("Please enter your name:", "");  
        if (user != "" && user != null) {  
            setCookie("username", user, 365);  
        }  
    }  
}  
checkCookie(); 

 

cookie的存取删

标签:log   else   char   function   cti   exp   ret   escape   etc   

原文地址:http://www.cnblogs.com/kewenxin/p/7723360.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!