日期:2014-05-16  浏览次数:20424 次

cookie读取问题
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">
var array=new Array("1","2","3");
document.write(array+"<br>");
document.cookie="username1="+encodeURI("用户名1");
document.cookie="username2="+encodeURI("用户名2");
document.cookie="username3="+encodeURI("用户名3");
for(i=0;i<10;i++)//1
{document.write(decodeURI("测试用的"+document.cookie[i])+"<br>");
}
if(document.cookie.length>0)
 { start=document.cookie.indexOf("username2=");
  if(start!=-1)
{start=start+10;
end=document.cookie.indexOf(";",start);
if(end==-1)
{end=document.cookie.length;  

document.write(decodeURI(document.cookie.substring(start,end)));
}
 }

</script>
</head>


这儿存储的cookie通过//1处可知是以单个字符的数组存储的,而实际上cookie是由连续的字符串组成,
应该直接以字符窜的形式存储啊,使用indexOf()表明cookie对象是一个字符窜,使用cookie.length
表明cookie对象一个数组,这个该怎么解释???????

------解决方案--------------------
//1
这里是一个字符串,
比如
s="abcdefghijk"
for(i=0;i<10;i++)//1
{document.write(decodeURI("测试用的"+s[i])+"<br>");
}

打印的是字符串中不同位置的字符,不是数组

字符串有length属性啊,不光是数组有