在设置和获取饼干方面遇到了一些麻烦

发布于 2025-01-23 03:43:57 字数 1280 浏览 3 评论 0原文

作为我的大学课程的一部分,我正在研究测验应用程序,并遇到了一些障碍。下面的代码是我写的几个函数。这些应该从名称和电子邮件字段中获取用户输入,验证电子邮件并将名称存储为要在另一页上显示的cookie。

我在大多数情况下都可以使用此功能,但是我很难获得存储的cookie值以正确显示。它正在加载,但显示为不确定= ryan。如果我键入更长的名称,则显示越来越少的字符串。我认为我试图将字符串分开的部分存在问题,但是我无法弄清楚这一生的一生。

任何帮助或建议将不胜感激。

谢谢 :)

function setCookie(cname, cvalue) 
{
    let userValue = document.getElementById("email").value;

    let pos_of_at = userValue.indexOf("@");

    if (userValue == " " || pos_of_at<0)
    {
        alert("You must enter a valid email address");
        document.getElementById("email").focus();
    }

    else
    {
        cvalue=document.getElementById("userName").value;

        document.cookie= cname + " = " + cvalue + ";";

        window.location.href="/quiz.html"
    }
}

function getCookie(cname) 
{
    let name = cname + "=";
    
    let decodedCookie = decodeURIComponent(document.cookie);

    let ca = decodedCookie.split(';');

    for(let i =0; i<ca.length; i++)
    {
        let c = ca[i];

        c = c.trim();

        if (c.indexOf(c) ==0)
        {
            return c.substring(name.length, c.length);
        }
    }

    return "cookie not found";
}


function checkCookie()
{
    document.getElementById("userName").innerHTML=getCookie("name");    
}

I'm working on a quiz app as part of my college course and have ran into a bit of a roadblock. The code below is for few functions I've wrote. These are supposed to take the user input from the name and email fields, validate the email and store the name as a cookie to be displayed on another page.

I've got this working for the most part, but I'm having trouble getting the stored cookie value to display correctly. It is loading, but is showing as undefined=ryan. If I type a longer name, less and less of the string is displayed. I think there's an issue with the section where I've tried to split the string, but I can't figure it out for the life of me.

Any help or advice would be greatly appreciated.

Thanks :)

function setCookie(cname, cvalue) 
{
    let userValue = document.getElementById("email").value;

    let pos_of_at = userValue.indexOf("@");

    if (userValue == " " || pos_of_at<0)
    {
        alert("You must enter a valid email address");
        document.getElementById("email").focus();
    }

    else
    {
        cvalue=document.getElementById("userName").value;

        document.cookie= cname + " = " + cvalue + ";";

        window.location.href="/quiz.html"
    }
}

function getCookie(cname) 
{
    let name = cname + "=";
    
    let decodedCookie = decodeURIComponent(document.cookie);

    let ca = decodedCookie.split(';');

    for(let i =0; i<ca.length; i++)
    {
        let c = ca[i];

        c = c.trim();

        if (c.indexOf(c) ==0)
        {
            return c.substring(name.length, c.length);
        }
    }

    return "cookie not found";
}


function checkCookie()
{
    document.getElementById("userName").innerHTML=getCookie("name");    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那些过往 2025-01-30 03:43:57

弄清楚了。问题是CheckCookie功能。

应该

function checkCookie()
{
    document.getElementById("userName").innerHTML=getCookie("name");    
}

function checkCookie()
{
    document.getElementById("userName").innerHTML=getCookie();    
}

Figured it out. Issue was the checkCookie function.

Was

function checkCookie()
{
    document.getElementById("userName").innerHTML=getCookie("name");    
}

Should have been

function checkCookie()
{
    document.getElementById("userName").innerHTML=getCookie();    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文