javascript读取Cookie的问题!

wzhxj2 2005-09-02 05:52:38
本人遇到难题,就是如何用javascript读取Cookie ?
请问javascript中有关cookie的属性、方法等都有哪些?
获取到cookie之后又如何判断,显示不同的信息!
各位说地越详细越好,因为本人对javascript了解的不是很多,
现在需要调用本地的程序,没办法才用javascript的,
谢谢!!!
...全文
208 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
amorsuper 2005-09-02
  • 打赏
  • 举报
回复
BASIC PRACTICE OF COOKIE:

document.cookie = mystring
mystring的格式:
name=value [; expires=date] [; domain=domain] [; path=path] [; secure]

example:
username=amor;expires=Thu, 01-Jan-1970 00:00:01 GMT;domain=www.csdn.net;path=/amor/;secure

常用方法和属性:
cookie.length
cookie[property-name]
cookie.indexOf()
cookie.subString()
cxz7531 2005-09-02
  • 打赏
  • 举报
回复
这是dw提供的经典代码,两个函数:一个读取cookie 一个写入

function readCookie(name)
{
var cookieValue = "";
var search = name + "=";
if(document.cookie.length > 0)
{
offset = document.cookie.indexOf(search);
if (offset != -1)
{
offset += search.length;
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
cookieValue = unescape(document.cookie.substring(offset, end))
}
}
return cookieValue;
}
// Example:
// writeCookie("myCookie", "my name", 24);
// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.
function writeCookie(name, value, hours)
{
var expire = "";
if(hours != null)
{
expire = new Date((new Date()).getTime() + hours * 3600000);
expire = "; expires=" + expire.toGMTString();
}
document.cookie = name + "=" + escape(value) + expire;
}
matrixy 2005-09-02
  • 打赏
  • 举报
回复
document.cookie属性

具有读写权限

给你代码一段,是俺们DHTML群里一家伙做的类,俺弄上来给你也瞧瞧...

///////////////////////////////////////////////////////////////////////////////////
<html>
<head>
<title> 新文档 </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="FlashSoft">
<meta name="Keywords" content="">
<meta name="Description" content="FlashSoft">
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
</head>
<body>
<script language="JavaScript" type="Text/JavaScript">
<!--
function Cookie(){
//Cookie.set(String sName,String sValue,[Date dHours,String sRegion,String sPath,String sDomain,Boolean bSecure])
this.set=function(sName,sValue,dHours,sRegion,sPath,sDomain,bSecure){
var str=new String();
var nextTime=new Date();
nextTime.setHours(nextTime.getHours()+dHours);// 设定时间往后推迟指定的小时数
sValue=escape(sValue);// 编码要出入的值
if(sRegion){// 如果有指定子项
var tValue=this.get(sName,"",true);// 获取大项里面的值[包含子项值]
var tStr=tValue.replace(new RegExp("\\b("+sRegion+"=)[^\\&]+\\b","i"),"$1"+sValue);// 在获取到的值里面进行查找替换,替换指定子项的值
if(RegExp.$1)// 如果替换成功
str=sName+"="+tStr;// 设定大项值
else// 如果替换不成功
str=sName+"="+sRegion+"="+sValue+(/=/.exec(tValue)?"&"+tStr:"");// 则把指定值写入,另,如查到以前已经设定了,那么把原值也写入
}
else// 如果没有指定子项
str=sName+"="+sValue;
if(dHours)// 如果指定超时时间
str+=";expires="+nextTime.toGMTString();
if(sPath)// 指定路径
str+=";path="+sPath;
if(sDomain)// 作用域
str+=";domain="+sDomain;
if(bSecure)// 是否未安全cookie
str+=";secure";
document.cookie=str;
}
//Cookie.get(String sName,[String sRegion,Boolean bCode])
this.get=function(sName,sRegion,bCode){
var rs=new RegExp("(^|)"+sName+"=([^;]*)(;|$)","g").exec(document.cookie);// 在大项里面查找
if(sRegion&&rs){// 如果指定小项,并且大项不为空
var rs1=new RegExp("(^|)"+sRegion+"=([^\&]*)(\&|$)","g").exec(rs[2]);// 在小项里面找
return rs1?bCode?rs1[2]:unescape(rs1[2]):"";// 返回小项值
}
else{
return rs?bCode?rs[2]:unescape(rs[2]):"";// 返回大项值
}
}
//Cookie.remove(String sName)
this.remove=function(sName){
this.set(sName,"",-1);
}
}

var _s=new Cookie();
_s.set("EyeSee","test",1,"subKey");

alert(document.cookie);
/*
var d=new Date()
d.setFullYear(d.getFullYear()+1);
document.cookie="abc=123;expires="+d.toGMTString();
alert(document.cookie);
*/
//-->
</script>
</body>
</html>

87,993

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧