为什么我的JavaScript cookie只能在一个页面上工作?
我有一个问题。我已经使用这个示例从以下站点编写和读取JavaScript cookie:。cookie对同一页的读写都很好,但一旦我转到另一个具有类似表单的页面,cookie中的信息就会消失 我最初是在桌面上使用它的,但一旦我将它添加到我们正在测试的开发站点上,它就只能在一个页面上工作。基本上,我可以在一个带有表单的页面上设置cookie,然后在另一个页面上就没有cookie可读了。但是,我可以在第二个表单上创建另一个cookie,它保存得很好。当我回到第一页表单时,我创建的第一个cookie填充表单字段 因此: 有关该网站的其他信息: Apache服务器 PHP5.4 AngularJS 1.2.26 介绍 其他JavaScript和jQuery文件 第三方脚本 当我调试document.cookie时,我在其中看到的唯一东西是phpsessid。这是否会阻止我的cookies被带到另一页的表单上?这些表单都在同一个域上,所以 与开发人员网站相同的桌面版本: 第1页为什么我的JavaScript cookie只能在一个页面上工作?,javascript,cookies,Javascript,Cookies,我有一个问题。我已经使用这个示例从以下站点编写和读取JavaScript cookie:。cookie对同一页的读写都很好,但一旦我转到另一个具有类似表单的页面,cookie中的信息就会消失 我最初是在桌面上使用它的,但一旦我将它添加到我们正在测试的开发站点上,它就只能在一个页面上工作。基本上,我可以在一个带有表单的页面上设置cookie,然后在另一个页面上就没有cookie可读了。但是,我可以在第二个表单上创建另一个cookie,它保存得很好。当我回到第一页表单时,我创建的第一个cookie填
<html>
<head>
<script src="tutorialspoint-cookies.js" type="text/javascript"></script>
</head>
<body>
<h1>FORM 1</h1>
<form name="form_000c" id="form_000c" action="">
<label>First Name:</label>
<input type="text" name="First_Name" id="First_Name" /><br />
<label>Last Name:</label>
<input type="text" name="Last_Name" id="Last_Name" /><br />
<label>Email:</label>
<input type="text" name="Email" id="Email" /><br />
<label>Phone Number:</label>
<input type="text" name="Phone" id="Phone" /><br />
<label>Timeline:</label>
<select name="Timeline" id="Timeline">
<option value="time1">Timeline 1</option>
<option value="time2">Timeline 2</option>
<option value="time3">Timeline 3</option>
<option value="time4">Timeline 4</option>
</select><br />
<label>Measurements:</label>
<select name="Measurements" id="Measurements">
<option value="meas1">Measurement 1</option>
<option value="meas2">Measurement 2</option>
<option value="meas3">Measurement 3</option>
<option value="meas4">Measurement 4</option>
</select><br />
<input type="button" value="Set Cookie" onclick="WriteCookie();"/>
</form>
<a href="tutorialspoint-cookies-2.html">go to page 2</a>
</body>
</html>
表格一
名字:
姓氏:
电邮:
电话号码:
时间线:
时间线1
时间线2
时间线3
时间线4
测量:
测量1
措施2
措施3
措施4
第2页
<html>
<head>
<script src="tutorialspoint-cookies.js" type="text/javascript"></script>
</head>
<body onLoad="ReadCookie()">
<h1>FORM 2</h1>
<form name="form_000c" id="form_000c" action="">
<label>First Name:</label>
<input type="text" name="First_Name" id="First_Name" /><br />
<label>Last Name:</label>
<input type="text" name="Last_Name" id="Last_Name" /><br />
<label>Email:</label>
<input type="text" name="Email" id="Email" /><br />
<label>Phone Number:</label>
<input type="text" name="Phone" id="Phone" /><br />
<label>Timeline:</label>
<select name="Timeline" id="Timeline">
<option value="time1">Timeline 1</option>
<option value="time2">Timeline 2</option>
<option value="time3">Timeline 3</option>
<option value="time4">Timeline 4</option>
</select><br />
<label>Measurements:</label>
<select name="Measurements" id="Measurements">
<option value="meas1">Measurement 1</option>
<option value="meas2">Measurement 2</option>
<option value="meas3">Measurement 3</option>
<option value="meas4">Measurement 4</option>
</select><br />
<input type="button" value="Set Cookie" onclick="WriteCookie();"/>
</form>
<a href="tutorialspoint-cookies.html">go to page 1</a>
</body>
</html>
表格二
名字:
姓氏:
电邮:
电话号码:
时间线:
时间线1
时间线2
时间线3
时间线4
测量:
测量1
措施2
措施3
措施4
JavaScript cookie
<!--http://www.tutorialspoint.com/javascript/javascript_cookies.htm
function WriteCookie(){
cookievalue1 = escape(document.form_000c.First_Name.value) + ";";
cookievalue2 = escape(document.form_000c.Last_Name.value) + ";";
cookievalue3 = escape(document.form_000c.Email.value) + ";";
cookievalue4 = escape(document.form_000c.Phone.value) + ";";
cookievalue5 = escape(document.form_000c.Timeline.value) + ";";
cookievalue6 = escape(document.form_000c.Measurements.value) + ";";
document.cookie = "First_Name=" + cookievalue1;
document.cookie = "Last_Name=" + cookievalue2;
document.cookie = "Email=" + cookievalue3;
document.cookie = "Phone=" + cookievalue4;
document.cookie = "Timeline=" + cookievalue5;
document.cookie = "Measurements=" + cookievalue6;
alert("Setting Cookies : " + "First_Name=" + cookievalue1 + "Last_Name=" + cookievalue2 + "Email=" + cookievalue3 + "Phone=" + cookievalue4 + "Timeline=" + cookievalue5 + "Measurements=" + cookievalue6 );
}
function ReadCookie(){
var allcookies = document.cookie;
// Get all the cookies pairs in an array
cookiearray = allcookies.split(';');
// Now take key value pair out of this array
for(var i=0; i<cookiearray.length; i++){
name = cookiearray[i].split('=')[0];
// the cookie is leaving a white space in the name so we need to remove it with .trim()
name = name.trim();
value = cookiearray[i].split('=')[1];
document.getElementById(name).value = value;
}
}
设置cookie时,请务必记住还需要指定路径
//在javascript中设置cookie时使用path=/
document.cookie = "First_Name=" + cookievalue1 + " path=/";
document.cookie = "Last_Name=" + cookievalue2 + " path=/";
document.cookie = "Email=" + cookievalue3 + " path=/";
document.cookie = "Phone=" + cookievalue4 + " path=/";
document.cookie = "Timeline=" + cookievalue5 + " path=/";
document.cookie = "Measurements=" + cookievalue6 + " path=/";
谢谢现在所有页面都在运行。我在最后一行做了一个更改,以避免任何其他cookie导致脚本失败
document.cookie = "First_Name=" + cookievalue1 + " path=/";
document.cookie = "Last_Name=" + cookievalue2 + " path=/";
document.cookie = "Email=" + cookievalue3 + " path=/";
document.cookie = "Phone=" + cookievalue4 + " path=/";
document.cookie = "Timeline=" + cookievalue5 + " path=/";
document.cookie = "Measurements=" + cookievalue6 + " path=/";