一个cookie上有多个值

时间:2015-10-02 04:49:41

标签: javascript cookies

我写的代码应该在一个cookie上存储多个用户值,但由于某种原因它不起作用。任何人都可以解释我的错误吗?

var delimiter = "#";

function createCookie(name, value, days, path, domain, secure) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    var expires = date.toGMTString();
  } else var expires = "";
  cookieString = name + "=" + escape(value);
  if (expires)
    cookieString += "; expires=" + expires;
  if (path)
    cookieString += "; path=" + escape(path);
  if (domain)
    cookieString += "; domain=" + escape(domain);
  if (secure)
    cookieString += "; secure";
  document.cookie = cookieString;
}

function getCookie(name) {
  var nameEquals = name + "=";
  var whole_cookie = document.cookie.split(nameEquals)[1].split(";")[0];
  var crumbs = whole_cookie.split(delimiter);
}

function deleteCookie(name) {
  createCookie(name, "", -1);
}

function myCookie() {
  var name = document.getElementById('name').value;
  var age = document.getElementById('age').value;
  var userdata = name + delimiter + age;
  createCookie("user", userdata);
  var myUser = getCookie('user');
  var myUserArray = myUser.split(delimiter);
  var cookieName = myUserArray[0];
  var cookieAge = myUserArray[1];
  alert('The cookie created has the following user information stored on it: \nName: ' + cookieName '\nAge: ' + cookieAge);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

Please enter your name
<input type=text id='name' />
<br>
<br>Please enter your age
<input type=text id='name' />
<input type=button value='Go!' onClick='myCookie()' />

1 个答案:

答案 0 :(得分:1)

您的代码中存在一些错误:

  1. getCookie中,您忘记返回Cookie的值!

    function getCookie(name) {
        var nameEquals = name + "=";
        var whole_cookie=document.cookie.split(nameEquals)[1].split(";")[0];         
        return whole_cookie;
    }
    
  2. 您有两个ID name的输入,我想您想将其中一个重命名为age

  3. 在尝试执行Javascript之前,请确保您的文档已完全加载!可以通过将代码包裹在document.body.onload = function(){ [your code here] }周围,或者在文档末尾移动<script>标记来实现。

  4. +中的cookieName'\nAge'之间缺少alert

  5. var delimiter = "#";
    
    function createCookie(name, value, days, path, domain, secure) {
      if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = date.toGMTString();
      } else var expires = "";
      cookieString = name + "=" + escape(value);
      if (expires)
        cookieString += "; expires=" + expires;
      if (path)
        cookieString += "; path=" + escape(path);
      if (domain)
        cookieString += "; domain=" + escape(domain);
      if (secure)
        cookieString += "; secure";
      document.cookie = cookieString;
    }
    
    function getCookie(name) {
      var nameEquals = name + "=";
      var whole_cookie = document.cookie.split(nameEquals)[1].split(";")[0];
      return whole_cookie;
    }
    
    function deleteCookie(name) {
      createCookie(name, "", -1);
    }
    
    function myCookie() {
      var name = document.getElementById('name').value;
      var age = document.getElementById('age').value;
      var userdata = name + delimiter + age;
      createCookie("user", userdata);
      var myUser = getCookie('user');
      var myUserArray = myUser.split(delimiter);
      var cookieName = myUserArray[0];
      var cookieAge = myUserArray[1];
      alert('The cookie created has the following user information stored on it: \nName: ' + cookieName + '\nAge: ' + cookieAge);
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    
    Please enter your name
    <input type=text id='age' />
    <br>
    <br>Please enter your age
    <input type=text id='name' />
    <input type=button value='Go!' onClick='myCookie()' />