JS 操作Cookies

Cookies

1:功能:保存一小块数据
2:常用Cookies属性:expires,path,domain,secure
3:写入Cookies:格式 name=value
4:用escape编码,unescape解码
5:用obj.setTime(obj.getTime+60*1000)设置过期时间
6:cookie保存的数据不能超过4000bytes

7:cookie读取的时候用indexOf,substring,或者split

JS操作cookies

<HTML>
<SCRIPT LANGUAGE="JavaScript">
function Set(){
    var Then = new Date()        
    Then.setTime(Then.getTime() + 60*1000 )   //60秒
    document.cookie = "Cookie1=测试数据;expires="+ Then.toGMTString() 
}

function Get(){  
    var cookieString = new String(document.cookie)
    var cookieHeader = "Cookie1="
    var beginPosition = cookieString.indexOf(cookieHeader)
    if (beginPosition != -1){
        document.all.Textbox.value = cookieString.substring(beginPosition 
            + cookieHeader.length) 
        }
    else
        document.all.Textbox.value = "Cookie 未找到!"   
}
</SCRIPT>
<BODY>
设置与读取 cookies...<BR>
<INPUT TYPE = BUTTON Value = "设置cookie" onClick = "Set()">
<INPUT TYPE = BUTTON Value = "读取cookie" onClick = "Get()"><BR>
<INPUT TYPE = TEXT NAME = Textbox>
</BODY>
</HTML>


你可能感兴趣的:(Cookies)