2016-03-05 43 views
0

我在嘗試理解Cookie如何在JavaScript中工作的可怕時間。JavaScript Cookie Assist

這裏就是我想要做的事: 寫了一個名爲「庫」的Cookie,並設置到期日21天, 使用警報讓用戶知道該cookie創建

重定向如果沒有錯誤發生(用戶名或密碼在表單上留空)並將消息發佈到「歡迎」,+用戶名「!」;的效果,則用戶轉到另一頁「blah.html」。如果發生錯誤,請重定向到blah2.html並提醒用戶錯誤。任何援助將不勝感激!提前致謝!

我的Cookie代碼:

function writeCookie() { 
    if (document.myForm.username.value == "") { 
    alert("You did not enter a user name!"); 
    window.location.href. = "home.html"; 
    } else if (document.myForm.pwd.value == "") { 
    alert("You did not enter a password!"); 
    window.location.href = "home.html"; 
    } else 
    cookieValue = escape(document.myForm.username.value) + ";"; 
    document.cookie = "name=" + cookieValue; 
    alert("Setting Cookies: " + "name=" + cookieValue); 
    window.location.href = "private.html"; 

} 

function getCookie() { 
    var userWelcome = document.cookie; 
} 

我的html代碼:

<p> Welcome to the Login Screen. To continue, please enter your username and password</p> 

<form name="myForm"> 
    <fieldset> 
    <legend><b>User Information</b></legend><br> 
    <label for="username">User Name:</label> 
    <input name="username" size="20" maxlength="20" type="text">&nbsp;&nbsp; 
    <label for="pwd">Password:</label> 
    <input name="pwd" size="25" maxlength="25" type="password"> 
    <br> 
    <br> 
    <center> 
     <input type="Submit" value="Login" id="login" onclick="createCookie 
    ();">&nbsp;&nbsp;<input value="Reset" type="reset"></center> 
    </fieldset> 
</form> 
+2

https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie/Simple_document.cookie_framework – epascarello

+0

你有一個語法錯誤這裏:'window.location.href。 =「home.html」;''href'後面的'.'是一個錯誤。 – Barmar

+0

你的問題說你要創建一個名爲'Location'的cookie,但是你的代碼創建了一個名爲'name'的cookie。此外,該函數的名稱是'writeCookie()',但HTML調用'createCookie()'。 – Barmar

回答

0

所以我搬來搬去一些東西,並清理錯誤。

有在這條線的錯誤:window.location.href. 應該是:window.location.href

<script> 
function writeCookie(){ 
try{ 
if(document.myForm.username.value == ""){ 
    alert("You did not enter a user name!"); 
    return false; 
    } else if(document.myForm.pwd.value == ""){ 
    alert("You did not enter a password!"); 
    return false; 
    } else { 
    cookieValue = escape(document.myForm.username.value) +";"; 
    document.cookie = "name=" + cookieValue; 
    alert("Setting Cookies: " + "name=" + cookieValue);  
    window.location.href = "private.html"; 
    } 
} catch(e){alert(e); return false;} 
} 

function getCookie(){ 
var userWelcome = document.cookie; 
} 
</script> 

<p> Welcome to the Login Screen. To continue, please enter your username and password</p> 

<form name="myForm" onsubmit="return writeCookie()" action="post"> 

<fieldset> 
<legend><b>User Information</b></legend><br> 
<label for="username">User Name:</label> 
<input name="username" size="20" maxlength="20" type="text">&nbsp;&nbsp; 
<label for="pwd">Password:</label> 
<input name="pwd" size="25" maxlength="25" type="password"> 
<br> 
<br> 

   

我把它用表格的的onsubmit行動,它將返回如果用戶沒有輸入用戶名和密碼而不是重定向,則爲false。 (我假設你將它重定向到同一頁面)。目前,您刪除錯誤後,您的代碼也會設置Cookie值。

這是一個JSFiddle來顯示它的工作。

要檢索的cookie根本就

var cookie = document.cookie; 

    var name = cookie.split('=')[1]; 
+0

我很感激幫助,現在我該如何去獲取cookie以在其他頁面上顯示用戶名(例如,您好,用戶!) – crizil

+0

如果它位於相同的域上,您應該能夠做到 var cookie = document 。曲奇餅; var name = cookie.split('=')[1] – kemiller2002