gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 20:15:48 27 4
gpt4 key购买 nike

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

最佳答案

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

关于Javascript Cookie 代码不存储 cookie/读取空 cookie 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6330986/

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