首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

JAVASCRIPT 无法读取COOKIE,该如何解决

2012-01-31 
JAVASCRIPT 无法读取COOKIE高手帮我看看这两个页面是一起使用的第一个页面没什么问题第二个页面无法读取写

JAVASCRIPT 无法读取COOKIE
高手帮我看看这两个页面是一起使用的第一个页面没什么问题第二个页面无法读取

写入的HTML(写入的页面非有问题)
<!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> 生成COOKIE </title>
<script   type= "text/javascript ">
<!--
function   writeCooke()
{
var   d   =   new   Date();
d.setTime(d.getTime()   +   24*60*60);
var   cookiestr   =   escape(document.getElementById( "mF ").name.value   +   "? "   +   document.getElementById( "mF ").sex.value   +   "? "   +   document.getElementById( "mF ").age.value   +   "? "   + document.getElementById( "mF ").prow.value   +   "? "   +   document.getElementById( "mF ").Email.value);
document.cookie   =   "UserInfo   =   "+cookiestr+ ";expires= "+d.toGMTString();
window.location   =   "显示Cookie.html ";
}
//-->
</script>
</head>

<body>
<form   id= "mF ">
<table   border= "0 ">
<tr>
<td>
姓名: <input   id= "name "   type= "text "   size= "15px "   />
</td>
</tr>
<tr>
<td>
性别: <input   id= "sex "   type= "text "   size= "15px "   />
</td>
</tr>
<tr>
<td>
年龄: <input   id= "age "   size= "15px "   />
</td>
</tr>
<tr>
<td>
省份:
<select   id= "prow ">
<option   value= "吉林 "> 吉林 </option>
<option   value= "北京 "> 北京 </option>
<option   value= "山东 "> 山东 </option>
<option   value= "上海 "> 上海 </option>
<option   value= "陕西 "> 陕西 </option>
<option   value= "河北 "> 河北 </option>
<option   value= "广西 "> 广西 </option>
</select>
</td>
</tr>
<tr>
<td>
E-mail: <input   type= "text "   size= "25px "   id= "Email "   />
</td>
</tr>
<tr   align= "center ">
<td>
<input   type= "button "   onclick= "writeCooke(); "   value= "完成 "   />
</td>
</tr>
</table>
</form>
</body>
</html>


读取的HTML页面(读取的页面无法正常读取)
var   cookiestr   =   document.cookie;
var   start   =   0;end   =   0;
if(cookiestr.indexOf( "UserInfo= ")   !=   -1)
{
start   =   cookiestr.indexOf( "UserInfo= ")   +   "UserInfo ".length;
end   =   cookiestr.indexOf( "; ");

var   theValue   =   usescape(cookiestr.substring(start+1,end));


var   endName   =   theValue.indexOf( "? ");
var   theName   =   theValue.substring(0,endName);

theValue   =   theValue.substring(endName   +   1);
var   endSex   =   theValue.indexOf( "? ");
var   theSex   =   theValue.substring(0,endSex);

theValue   =   theValue.substring(endSex   +   1);
var   endAge   =   theValue.indexOf( "? ");
var   theAge   =   theValue.substring(0,endAge);

theValue   =   theValue.substring(endAge   +   1);
var   endProv   =   theValue.indexOf( "? ");
var   theProv   =   theValue.substring(0,endProv);

theValue   =   theValue.substring(endProv   +   1);
var   theEmail   =   theValue.substring(0);

document.write( "您输入的信息是: ");
document.write( " <table   border= "0 "> <tr> <td> 姓名: </td> <td> "   +   theName   +   " </td> </tr> ");
document.write( " <tr> <td> 性别: </td> <td> "   +   theSex   +   " </td> </tr> ");
document.write( " <tr> <td> 年龄: </td> <td> "   +   theAge   +   " </td> </tr> ");
document.write( " <tr> <td> 省份: </td> <td> "   +   theProv   +   " </td> </tr> ");
document.write( " <tr> <td> Email: </td> <td> "   +   theEmail   +   " </td> </tr> ");
}

[解决办法]
第二个页没有解码当然取不到了if(cookiestr.indexOf( "UserInfo= ") != -1)之前cookiestr = document.unescape(cookiestr);

热点排行