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

时间:2017-08-09 17:35:46

标签: javascript cookies query-string

我正在处理一个项目,我必须从第1页(名称,ID和学期)获取信息并将其存储在查询字符串中。然后我必须获取该信息并在第2页中存储它,并在第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;
    }

1 个答案:

答案 0 :(得分:1)

由于当您在本地开发时,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");