`

javascript小甜饼_cookie方法

    博客分类:
  • ajax
阅读更多
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>简单HTML</title>
<meta name="author" content="Yeeku.H.Lee" />
<meta name="website" content="http://www.crazyjava.org" />
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />


</head>
<body>
<script type="text/javascript">
//写Cookie
function createCookie(name,value,days)
{
    if (days)
    {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    var temp = name+"="+value+expires+"; path=/";
//书名=2020; expires=Wed, 22 Sep 2010 05:32:31 UTC; path=/
//document.writeln(temp);
document.cookie = temp;
}
//读Cookie
function readCookie(name)
{
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
//:书编号=100859
//document.writeln("::"+ca);

    for(var i=0;i < ca.length;i++)
    {
        var c = ca[i];
//name=tom
        //user=tom
        //我是Cookie名称=我是Cookie内容
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}
//清Cookie
function eraseCookie(name)
{
    createCookie(name,"",-1);
}

//调用示例

//createCookie("书编号","100859",365);
//alert(readCookie('书编号'));
//eraseCookie('书编号');
</script>
</body>
</html>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics