Javascript Cookie 代码不存储 cookie/读取空 cookie 值?

发布于 2024-11-15 05:08:13 字数 1564 浏览 4 评论 0原文

我从 Headfirst Javascript 书中获取了用于 cookie 的代码。但由于某种原因,它不适用于我的浏览器。我主要使用chrome和ff,并且我在chrome中启用了本地cookie。帮助??

<script type="text/javascript">


function checkCookie(){
    var name = eatCookie("yahooEmail");
    if(name)
        document.getElementById("emailVerf").style.visibility="hidden";
    else
        document.getElementById("emailBody").style.visibility="hidden";
}



function writeCookie(name, val, days){

    var expires = "";

    if(days){
        var date = new Date();
        date.setTime(date.getTime + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    }

    document.cookie = name + "=" + val + expires + "; path=/";

}

function eatCookie(val){
    var search = val + "=";
    var cookies = document.cookie.split(';');

    for(var i=0; i < cookies.length; i++){
        var c = cookies[i];
        while(c.charAt(0) == ' ')
            c=c.substring(1, c.length);
        if(c.indexOf(search) == 0)
            return c.substring(search.length, c.length);
    }
    return null;

}

function verEmail(val){

    var regex = "[a-zA-Z0-9][@yahoo.com]";
    var exp = new RegExp(regex);

    if(!exp.test(val)){
        document.getElementById("email").style.color="red";
        alert("Please enter a valid Yahoo email address.");
        }
    else{
        writeCookie("yahooEmail", val, 7);
        document.getElementById("emailVerf").style.visibility="hidden";
        document.getElementById("emailBody").style.visibility="visible";
        }
}

</script>

I took this code from the Headfirst Javascript book, for cookies. But for some reason it's not working with my browsers. I'm mainly using chrome and ff, and i have local cookies enabled in chrome. Help??

<script type="text/javascript">


function checkCookie(){
    var name = eatCookie("yahooEmail");
    if(name)
        document.getElementById("emailVerf").style.visibility="hidden";
    else
        document.getElementById("emailBody").style.visibility="hidden";
}



function writeCookie(name, val, days){

    var expires = "";

    if(days){
        var date = new Date();
        date.setTime(date.getTime + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    }

    document.cookie = name + "=" + val + expires + "; path=/";

}

function eatCookie(val){
    var search = val + "=";
    var cookies = document.cookie.split(';');

    for(var i=0; i < cookies.length; i++){
        var c = cookies[i];
        while(c.charAt(0) == ' ')
            c=c.substring(1, c.length);
        if(c.indexOf(search) == 0)
            return c.substring(search.length, c.length);
    }
    return null;

}

function verEmail(val){

    var regex = "[a-zA-Z0-9][@yahoo.com]";
    var exp = new RegExp(regex);

    if(!exp.test(val)){
        document.getElementById("email").style.color="red";
        alert("Please enter a valid Yahoo email address.");
        }
    else{
        writeCookie("yahooEmail", val, 7);
        document.getElementById("emailVerf").style.visibility="hidden";
        document.getElementById("emailBody").style.visibility="visible";
        }
}

</script>

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

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

发布评论

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

评论(1

隔岸观火 2024-11-22 05:08:13

您正在编写一个名为“ayahooEmail”的 cookie,但正在读取一个名为“yahooEmail”的 cookie

You are writing a cookie named "ayahooEmail" but reading one called "yahooEmail"

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