2012-02-01 63 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