我在嘗試理解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">
<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
();"> <input value="Reset" type="reset"></center>
</fieldset>
</form>
https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie/Simple_document.cookie_framework – epascarello
你有一個語法錯誤這裏:'window.location.href。 =「home.html」;''href'後面的'.'是一個錯誤。 – Barmar
你的問題說你要創建一個名爲'Location'的cookie,但是你的代碼創建了一個名爲'name'的cookie。此外,該函數的名稱是'writeCookie()',但HTML調用'createCookie()'。 – Barmar