cookie學習筆記2

  在C語言中用struct結構自由存值,感覺不錯,

  在JAVA中class裏的set /get 存值,好像沒有人不知道了,

  在cookie中,也可以玩set /get  。

直接代碼了:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Cookie練習2</title>
</head>
<body>   
<script language="javascript" type="text/javascript">  
<!--
function dwn(s)
{
 document.write(s + "<br/>");
}
//定義一個Cookie對象
function Cookie()
{
 //其實這裏和練習1中的例子也沒有太大的區別,只是將setCookie()和getCookie函數封裝成了
 //設置cookie內容
this.set=function(name,value,expireTime){
 //將信息拼裝成cookie字符串保存
        if(!expireTime)expireTime=new Date();
        document.cookie=name+"="+value+";"+"expire="+expireTime.toGMTString();
};
//根據name獲取cookie信息
this.get=function(name){
 //拆分和解析cookie字符串
        var cookies=document.cookie.split("; ");
        for(var i=0;i<cookies.length;i++)
        {
   //拆分name和value
            var s=cookies[i].split("=");
   //匹配name並返回相應的value
            if(s[0]==name)return s[1];
        }
    }
}
//新建一個Cookie對象
var cookie=new Cookie();
//寫入a、b、c三個cookie值
cookie.set("a","15");
cookie.set("b","25");
cookie.set("c","35");
//讀取b和c的cookie值
dwn("b=" + cookie.get("b"));
dwn("c=" + cookie.get("c"));
-->
</script>  
</body>   
</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章