2013-07-14 199 views
1

我正在編寫基於教程和代碼工程的教程的代碼,但是當我自己嘗試這樣做時,它不會。你可以幫我嗎 ?Javascript cookies代碼不能正常工作

該代碼:(Tutorial Link)。

<!DOCTYPE html> 
<html> 
    <head> 
     <script type="text/javascript"> 
      var user_name; 
      var expires = new Date(); 
      if ((document.cookie == "") == false) 
      { 
       var length = document.cookie.length - 1; 
       var message = document.cookie.substr(5, length); 
       document.write("<h2><center>Welcome back, " + message + "</center></h2>"); 
      } 

      function check() 
      { 
       user_name = document.getElementById("name").value; 
       expires.setFullYear(expires.getFullYear() + 1); 
       document.cookie = escape("name") + escape(user_name) + "; expires = " + expires.toGMTString(); 
       alert(document.cookie); 
      } 
     </script> 
     <meta charset="utf-8"> 
     <title></title> 
    </head> 
    <body> 
     Name: <input type="text" id="name" /> 
     <input type="button" value="Enter" onClick="check()" /> 
    </body> 
</html> 
+2

這就是窮人的教程。你能鏈接到它嗎? – alex

+0

是的,如果代碼是從教程中獲得的,那麼一定要找到另一個。 – Pointy

+0

不要浪費時間來實現這一點。使用jquery.cookie https://github.com/carhartl/jquery-cookie – erdimeola

回答

1

因爲你所提到的I was creating this code that is tutorial based and on the tutorial,所以如果它只是一個test myself的事情,那麼你可以考慮使用此一(Demo Here),只是在cookie名稱和cookie值cookie的輸入名稱和值字段,當你想要Add一個新的cookie和Delete一個cookie只需在cookie名稱字段中輸入cookie的名稱,並且對於View相同,則只需通過使用&分割cookie並添加一個Delete All按鈕,撥打eraseCookie()功能,自己試試。

此外,網上已有better ones在真實情況下使用,但這一個並不壞。

function createCookie(name,value,days) { 
    if (days) { 
     var date = new Date(); 
     date.setTime(date.getTime()+(days*24*60*60*1000)); 
     var expires = "; expires="+date.toGMTString(); 
    } 
    else var expires = ""; 
    document.cookie = name+"="+value+expires+"; path=/"; 
    return true; 
} 

function readCookie(name) { 
    var nameEQ = name + "="; 
    var ca = document.cookie.split(';'); 
    for(var i=0;i < ca.length;i++) { 
     var c = ca[i]; 
     while (c.charAt(0)==' ') c = c.substring(1,c.length); 
     if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); 
    } 
    return null; 
} 

function eraseCookie(name) { 
    if(!name) { 
     alert('\nPlease enter the name of cookie in cookie the name field.'); 
     return false; 
    } 
    if(readCookie(name)){ 
     if(createCookie(name,"",-1)) { 
      alert('Cookie "' + name + '" has been deleted!');   
     } 
    } 
    else alert('Cookie "' + name + '" doesn\'t exist!'); 
} 

function addCookie() 
{ 
    cookie_name = document.getElementById("cName").value.replace(/^\s+|\s+$/g,''); 
    cookie_value = document.getElementById("cValue").value.replace(/^\s+|\s+$/g,''); 
    if(cookie_name.length && cookie_value.length){ 
     createCookie(cookie_name, cookie_value, 7); 
     alert("New cookie has been added, \ncookie name : " + cookie_name + "\ncookie value : " + cookie_value); 
    } 
    else{ 
     alert("Please enter a name and value for the cookie."); 
    } 
} 

function showCookie(name) 
{ 
    if(!name) { 
     alert('\nPlease enter the name of cookie in the cookie name field.'); 
     return false; 
    } 
    var val = readCookie(name); 
    if(val){ 
     alert(readCookie(name)); 
    } 
    else alert('Cookie "' + name + '" doesn\'t exist!'); 
} 

// Onload to check and greet 
if(name = readCookie('name')) alert('Welcome back, ' + name); 

Example Here.

+0

有人可以告訴我這個代碼中的問題是 – user2580136

+1

@ user2580136,怎麼了?這段代碼在givel fiddle鏈接中運行。 –