gpt4 book ai didi

Javascript--如何将解析后的查询字符串存储到cookie中

转载 作者:行者123 更新时间:2023-11-30 21:16:50 25 4
gpt4 key购买 nike

我正在做一个项目,我必须从第 1 页获取信息(姓名、ID 和学期)并将其存储在查询字符串中。然后我必须获取该信息并将其存储在第 2 页的 cookie 中,并在第 3 页显示它。我相信我已经将它正确地存储在查询字符串中。我已将其(暂时)设置为警报消息,只是为了确保它正常工作。警报显示在第 2 页(成功!!!)。我如何将这些解析后的信息存储到 cookie 中,以便我可以在第 3 页上显示?我需要存储和显示第 1 页和第 2 页的信息,并将其显示在第 3 页上。

功能页面1:

function querySemester() {
var saveSemester = location.search;
var semesterData = "";
if (saveSemester != "")
semesterData = saveSemester.substring(saveSemester.search("&FirstName"), saveSemester.length);
saveSemester = "?semester=" + semesterDisplay;
location.href = "RegistrationPage_2.html" + saveSemester;

函数页面 2:

function submitRegistration() {
var course = document.registration.courses.value;
var section = document.registration.section.value;
var major = document.registration.needForMajor.value;
//To display major requirement in confirm message
var checkDisplay;
if (document.registration.needForMajor.checked == true) {
checkDisplay = "Course Needed For Major";
}
else {
checkDisplay = "";
}
//Validates course
if (course == "") {
window.alert("You must select a Course!");
return false;
}
//Validates section
if (section == "") {
window.alert("You must select a Section!");
return false;
}
// Validates that the course and section are compatible
var error = true;
switch (course) {
case "CIS 100":
if (section == '100001') {
} else if (section == '100gw1') {
} else {
window.alert("You must select a valid section for CIS 100!");
error = false;
return false;
}
break;
case "CIS 120":
if (section == '120001') {
} else if (section == '120gw1') {
} else {
window.alert("You must select a valid section for CIS 120!");
error = false;
return false;
}
break;
case "CIS 220":
if (section == '220001') {
} else if (section == '220gw1') {
} else {
window.alert("You must select a valid section for CIS 220!");
error = false;
return false;
}
break;
case "CIS 299":
if (section == '299001') {
} else if (section == '299gw1') {
} else {
window.alert("You must select a valid section for CIS 299!");
error = false;
return false;
}
break;
case "CIS 302":
if (section == '302gw1') {
} else {
window.alert("You must select a valid section for CIS 302!");
error = false;
return false;
}
break;
case "CIS 304":
if (section == '304001') {
} else if (section == '304gw1') {
} else {
window.alert("You must select a valid section for CIS 304!");
error = false;
return false;
}
break;
case "CIS 321":
if (section == '321001') {
} else if (section == '321gw1') {
} else {
window.alert("You must select a valid section for CIS 321!");
error = false;
return false;
}
break;
case "CIS 322":
if (section == '322gw1') {
} else {
window.alert("You must select a valid section for CIS 322!");
error = false;
return false;
}
break;
case "CIS 325":
if (section == '325gw1') {
} else {
window.alert("You must select a valid section for CIS 325!");
error = false;
return false;
}
break;
case "CIS 330":
if (section == '330001') {
} else if (section == '330gw1') {
} else {
window.alert("You must select a valid section for CIS 330!");
error = false;
return false;
}
break;
case "CIS 332":
if (section == '332001') {
} else if (section == '332gw1') {
} else {
window.alert("You must select a valid section for CIS 332!");
error = false;
return false;
}
break;
case "CIS 341":
if (section == '341001') {
} else if (section == '341gw1') {
} else {
window.alert("You must select a valid section for CIS 341!");
error = false;
return false;
}
break;
case "CIS 343":
if (section == '34301a') {
} else if (section == '34301b') {
} else {
window.alert("You must select a valid section for CIS 343!");
error = false;
return false;
}
break;
case "CIS 352":
if (section == '352gw1') {
} else {
window.alert("You must select a valid section for CIS 352!");
error = false;
return false;
}
break;
case "CIS 354":
if (section == '354001') {
} else if (section == '354gw1') {
} else {
window.alert("You must select a valid section for CIS 354!");
error = false;
return false;
}
break;
case "CIS 401":
if (section == '401gw1') {
} else {
window.alert("You must select a valid section for CIS 401!");
error = false;
return false;
}
break;
case "CIS 419":
if (section == '419x01') {
} else {
window.alert("You must select a valid section for CIS 419!");
error = false;
return false;
}
break;
case "CIS 490":
if (section == '490001') {
} else if (section == '490gw1') {
} else {
window.alert("You must select a valid section for CIS 490!");
error = false;
return false;
}
break;
case "CIS 492":
if (section == '492gw1') {
} else {
window.alert("You must select a valid section for CIS 492!");
error = false;
return false;
}
break;
case "MAT 195":
if (section == '195001') {
} else if (section == '195w01') {
} else {
window.alert("You must select a valid section for MAT 195!");
error = false;
return false;
}
break;
case "MAT 215":
if (section == '215001') {
} else if (section == '215w01') {
} else {
window.alert("You must select a valid section for MAT 215!");
error = false;
return false;
}
break;
case "MAT 225":
if (section == '225001') {
} else if (section == '225w01') {
} else {
window.alert("You must select a valid section for MAT 225!");
error = false;
return false;
}
break;
case "MAT 281":
if (section == '281001') {
} else if (section == '281w01') {
} else {
window.alert("You must select a valid section for MAT 281!");
error = false;
return false;
}
}
var querySemester = location.search;
querySemester = querySemester.substring(1, querySemester.length);
var queryArray = querySemester.split("&");
for (var i = 0; i < queryArray.length; ++i) {
window.alert(queryArray[i] + "<br />");
}
return true;
}
function queryInfo() {
var saveInfo = location.search;
var data = "";
if (saveInfo != "")
data = saveInfo.substring(saveInfo.search("&FirstName"), saveInfo.length);
saveInfo = "?semester=" + semesterDisplay;
location.href = "GreendaleVerification.html" + saveInfo;
}

最佳答案

由于在本地开发时 cookie 并不总是有效(过去开发时我无法在 Chrome 中使用它们),我建议使用 localStorage:

这是在 localStorage 中设置值的方式:

localStorage.setItem("<attribute>", "<data>");

这就是您从 localStorage 获取值的方式:

var data = localStorage.getItem("<attribute>");
//code to do something with data

因此,对于您的代码,您可能希望将数据存储在第 1 页的代码中,如下所示:

localStorage.setItem("data", semesterData);
//it seemed like the data you passed to the URL was in semesterData, but
//you can pass any JavaScript string into the localStorage.setItem() command

然后在第2页和第3页用这个命令获取:

var data = localStorage.getItem("data");

关于Javascript--如何将解析后的查询字符串存储到cookie中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45597239/

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