Javascript Cookie Code not storing a cookie/reading a null cookie value?
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>
You are writing a cookie named "ayahooEmail" but reading one called "yahooEmail"
精彩评论