日期:2009-03-16  浏览次数:20904 次

请看下面用javascript设置和读取cookie的简单例子,现在的问题是,如果要设置的是一个cookie集,比如在cookie1集中有uname,uid两组信息,应该如何写呢?
cookie("cookie1")("uname")="testuser"
cookie("cookie1")("uname")="testuid"

------------------------------------------------------------------------
<HTML>
<BODY>
设置与读取 cookies...<BR>
<INPUT TYPE = BUTTON Value = "设置cookie" onClick = "Set()">
<INPUT TYPE = BUTTON Value = "读取cookie" onClick = "Get()"><BR>
<INPUT TYPE = TEXT NAME = Textbox>
</BODY>


<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>
</HTML>