2012-02-01 68 views
0

我正在学习Javascript cookie。基本上我想要的是处理Javascript Cookie

  1. 当第一次用户输入他们的人数将保存cookie名称,值和到期日期365

  2. 当用户再次访问我的网站,他/她并不只要cookie仍然存在或尚未在浏览器中删除,就不必再输入他的号码,他/她将被重定向到我的主页。

这里是我的JavaScript代码至今:

function checkCookie() { 
    var mob_tel=getCookie("mob_tel"); 
    if (mob_tel!=null && mob_tel!="") { 
     alert("Welcome again " + mob_tel); 
    } else { 
     set_name(""); 
    } 
} 

function set_name(form) { 
    var mob_tel = form.mobtel.value 
    if (mob_tel != "") { 
     if (confirm("Are you sure you want this saved as your number?")) { 
      setCookie ("mob_tel", mob_tel, 365); 
      //window.history.go(0); 
     } 
    } else alert("Geez, at least enter something, entering nothing will cause an error."); 
} 

我的html身体

<body onload="checkCookie()"> 
    <form> 
      Enter Mobile Number: <input type="text" name="mobtel"><br> 
      <input type="submit" value="Save Cookie" onclick="set_name(this.form)"> 
    </form> 
</body> 

它所有的作品,但在我的萤火虫:

Uncaught TypeError: Cannot read property 'value' of undefined 
set_name 
checkCookie 
(anonymous function) 
onload 

也许我的编码风格是错误的。我打开重写我的代码。我还是新来的JavaScript。

回答

0

这里有一个工作版本。

这就是我所做的:
1 - getCookie()setCookie()未定义。我使用W3Schools函数来补充它们。
2 - 有一些脚本错误(缺少分号等)我修正了这些。
3 - 对于set_name()函数,我将其更改为DOM查询以访问保存电话号码的输入。

<script> 
function checkCookie() { 
    var mob_tel=getCookie("mob_tel"); 
    if (mob_tel!=null && mob_tel!="") { 
     alert("Welcome again " + mob_tel); 
    } else { 
     set_name(""); 
    } 
} 

function getCookie(c_name) { 
    var i,x,y,ARRcookies=document.cookie.split(";"); 
    for (i=0;i<ARRcookies.length;i++) { 
     x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("=")); 
     y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1); 
     x=x.replace(/^\s+|\s+$/g,""); 
     if (x==c_name) { 
      return unescape(y); 
     } 
    } 
} 

function setCookie(c_name,value,exdays) { 
    var exdate=new Date(); 
    exdate.setDate(exdate.getDate() + exdays); 
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()); 
    document.cookie=c_name + "=" + c_value; 
} 

function set_name(form) { 
    var mob_tel = document.getElementById('mobtel').value; 
    if (mob_tel != "") { 
     if (confirm("Are you sure you want this saved as your number?")) { 
      setCookie ("mob_tel", mob_tel, 365); 
      //window.history.go(0); 
     } 
    } else alert("Geez, at least enter something, entering nothing will cause an error."); 
} 
</script> 
<body onload="checkCookie()"> 
    <form> 
      Enter Mobile Number: <input type="text" name="mobtel" id="mobtel"><br> 
      <input type="submit" value="Save Cookie" onclick="set_name(this.form)"> 
    </form> 
</body> 
1

添加名字你形成:

 

<form name="your_form_name_here"> 
    Enter Mobile Number: <input type="text" name="mobtel" value=""><br> 
    <input type="submit" value="Save Cookie" onclick="set_name(this.form)"> 
</form> 
 
+0

如何说firebug错误:Uncaught TypeError:无法读取未定义的属性'值'。这只有在用户仍然没有输入他的号码并将cookie保存在浏览器中时才会发生。 – 2012-02-01 05:03:07