2016-11-18 71 views
0

我一直在嘗試創建登錄頁面。它會檢查用戶名是否已經有一個cookie,如果是的話,它會要求輸入登錄信息。否則它會通過註冊程序。但是,這不起作用。它只是讓代碼出現在控制檯上。我完整的代碼在GitHub上的鏈接https://github.com/TheNumnut/Wars-of-Shares/blob/master/Login/index.html 我認爲這是不工作的我的代碼部分是:Cookie不會通過javascript命令執行提示和提醒

function checkCookie(checkusername, checkpassword, register) { 
    if (getCookie("username") != "") { 
    setCookie("username", checkusername, 365); 
    setCookie("password", checkpassword, 365); 
    } 
    else { 
    if (checkusername != getCookie("username") { 
     alert("Username wrong"); 
     window.close(); 
    } 
    else { 
     if (checkpassword != getCookie("password") { 
     alert("Password wrong"); 
     window.close(); 
    window.open("https://thenumnut.github.io/Wars-of-Shares/", "_self"); 
} 

</script> 
</head> 
<body> 
var username = prompt("Username: "); 
var password = prompt("Password: "); 
checkCookie(username, password); 

請告訴我,如果有任何問題。打印到網站的部分來自var username = prompt(「Username:」);直到checkCookie(用戶名,密碼);. 任何幫助將不勝感激。

+0

'window.close()的'會的其餘部分之前關閉當前'window'代碼被執行。 – PHPglue

+0

是的,當你在身體內寫入JavaScript代碼時,它周圍沒有腳本元素,它將被顯示而不是執行。如果這真的讓你感到意外,我認爲你不應該讓任何接近登錄功能的地方。 – CBroe

回答

0

你錯過了一個額外的),下面幾行

if (checkusername != getCookie("username") { 

if (checkpassword != getCookie("password") { 
0

由於您致電「checkCookie(username,password);」函數在html中不在javascript中。還有一些括號丟失。

<script> 
function checkCookie(checkusername, checkpassword, register) { 
    if (getCookie("username") != "") { 
     setCookie("username", checkusername, 365); 
     setCookie("password", checkpassword, 365); 
    } 
    else { 
     if (checkusername != getCookie("username")){ 
      alert("Username wrong"); 
      window.close(); 
     } 
     else { 
      if (checkpassword != getCookie("password")) { 
       alert("Password wrong"); 
       window.close(); 
       window.open("https://thenumnut.github.io/Wars-of-Shares/", "_self"); 
      } 
     } 
    } 
} 
checkCookie(username, password); 
</script> 
相關問題