2015-03-03 65 views
0

我的問題是我想調用一個函數來在不同的函數中創建一個cookie。出於某種原因,createCookie函數不會執行。在我打電話的createCookie(名稱)Javascript在不同功能中創建Cookie

功能

function login() { 
    var user = document.getElementById('name').value; 
    var pass = document.getElementById('pass').value; 
     if(user='Jacob',pass='password') { 
     createCookie('name','Jacob',0); 
     }else{ 
     alert('Invalid Credentials'); 
    } 
} 

的createCookie的(名字)功能

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=/"; 
} 

當我試圖找到使用這些功能時,顯示爲空值。

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 display(){ 
    var x = readCookie('name'); 
    alert(x); 
} 

回答

0

據我所看到的,你的代碼存儲和檢索cookie的就好了,但有一個大錯誤在你的代碼:

if(user='Jacob',pass='password') { 

這意味着「分配給'Jacob'user,將'password'指定爲pass,然後生成值'password'「(將始終將其視爲true)。

你需要的是:

if(user === 'Jacob' && pass === 'password') { 

另外,不要在大括號吝嗇。他們中有很多可以繞過,而且你的代碼在沒有它們的情況下會變得混亂。下面似乎很好地工作:

function login() { 
    var user = document.getElementById('name').value; 
    var pass = document.getElementById('pass').value; 
    if (user === 'Jacob' && pass === 'password') { 
     createCookie('name', 'Jacob', 0); 
    } else { 
     alert('Invalid Credentials'); 
    } 
} 

function createCookie(key, 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 = key + "=" + value + expires + "; path=/"; 
} 

function readCookie(key) { 
    var keyEq = key + "="; 
    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 display() { 
    var x = readCookie('name'); 
    alert(x); 
} 

還要注意的是readCookie可以替換爲:

function readCookie(key) { 
    return document.cookie.split(';').map(function (item) { 
     return item.split('=').map(decodeURIComponent); 
    }).filter(function (item) { 
     return item[0] && item[0].trim() === key; 
    }).map(function (item) { 
     return item[1]; 
    })[0]; 
} 
+0

感謝您的幫助。 – Jacob 2015-03-03 05:27:39

0

下面的代碼是working-

var createCookie = function(name, value, days) { 
    var expires; 
    if (days) { 
     var date = new Date(); 
     date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); 
     expires = "; expires=" + date.toGMTString(); 

    } 
    else { 
     expires = ""; 
    } 
    document.cookie = name + "=" + value + expires + "; path=/"; 
} 

function getCookie(c_name) { 
    if (document.cookie.length > 0) { 
     c_start = document.cookie.indexOf(c_name + "="); 
     if (c_start != -1) { 
      c_start = c_start + c_name.length + 1; 
      c_end = document.cookie.indexOf(";", c_start); 
      if (c_end == -1) { 
       c_end = document.cookie.length; 
      } 
      return unescape(document.cookie.substring(c_start, c_end)); 
     } 
    } 
    return ""; 
} 
createCookie('username','Jacob',0); 
alert(getCookie('username')); 

小提琴URL-https://jsfiddle.net/zr6h8nf7/3/

+0

這不是理由 – Jacob 2015-03-03 04:10:19

+0

@Jacob,上面的代碼工作,我在小提琴試用了提供鏈接,請檢查這是否也適用於您 – 2015-03-03 04:19:11

+0

這不是退出我正在尋找。我想在按下按鈕並且條件爲真時創建cookie。雖然代碼中條件爲真,但creatCooke函數將被忽略。 – Jacob 2015-03-03 04:25:26