【问题标题】:JavaScript Cookie AssistJavaScript Cookie 辅助
【发布时间】:2016-03-05 00:06:37
【问题描述】:

我很难理解 cookie 在 JavaScript 中的工作原理。

这就是我想要做的事情: 写出一个名为“Library”的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>

【问题讨论】:

  • 这里有语法错误:window.location.href. = "home.html"; .href 之后是一个错误。
  • 您的问题是要创建一个名为 Location 的 cookie,但您的代码会创建一个名为 name 的 cookie。另外,函数的名字是writeCookie(),但是HTML调用createCookie()

标签: javascript cookies


【解决方案1】:

所以我移动了一些东西并清理了错误。

此行出现错误: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];

【讨论】:

  • 感谢您的帮助,现在我将如何让 cookie 在其他页面上显示用户名,例如(您好,用户!)
  • 如果它在同一个域上,你应该可以做 var cookie = document.cookie; var name = cookie.split('=')[1]
猜你喜欢
  • 2014-04-26
  • 2011-07-25
  • 2011-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多