用JAVASCRIPT设置和读取COOKIE的简单例子
类别: JavaScript教程
请看下面用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>
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>
- 上一篇: 利用JAVASCRIPT实现时间段的查询
- 下一篇: 一个鼠标自动移动的JS例子
-= 资 源 教 程 =-
文 章 搜 索