gpt4 book ai didi

javascript - 为什么我的 JavaScript cookie 只能在一页上工作?

转载 作者:行者123 更新时间:2023-12-02 14:45:43 25 4
gpt4 key购买 nike

我有一个 JavaScript cookie 问题。我使用了从该站点写入和读取 JavaScript cookie 的示例:http://www.tutorialspoint.com/javascript/javascript_cookies.htm 。 cookie 在同一页面上读取和写入效果很好,但一旦我转到另一个具有类似形式的页面(它应该可以正常工作),cookie 中的信息就消失了。

我最初让它在我的桌面上工作,但一旦我将它添加到我们正在测试的开发站点上 - 它只能在一个页面上工作。基本上我可以使用表单在一个页面上设置 cookie,然后在另一页面上将没有 cookie 可供读取。不过,我可以在第二种形式上创建另一个 cookie,它保存得很好。当我返回到一个表单页面时 - 我创建的第一个 cookie 会填充表单字段。

所以:

Form 1 page - cookie 1 created 
- then go to -
Form 2 page - cookie 1 doesn't exist but I can create cookie 2
- then go to -
Form 1 page - cookie 1 loads into form 1
- then go to -
Form 2 page - cookie 2 loads into form 2

有关网站的其他信息:

Apache 服务器PHP 5.4AngularJS 1.2.26网络服务其他 JavaScript 和 jQuery 文件第 3 方脚本

当我调试它时,我在 document.cookie 中看到的唯一东西是 phpsessid。这是否会阻止我的 cookie 被转移到另一页的表格上?这些表单都在同一个域中,所以...

桌面版与DEV网站相同:

第 1 页

<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>

第 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>

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=/";

关于javascript - 为什么我的 JavaScript cookie 只能在一页上工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36633235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com