如何使用cookie删除某些字符?

时间:2017-03-06 03:58:54

标签: javascript cookies

我正在尝试打印已存储的Cookie。例如,如果我输入John Smith,那么它应该如下所示:

Welcome John Smith

然而,当它打印时,它看起来像这样:

Welcome firstname=John lastname=Smith

我不知道如何摆脱cookiename和标志=。我尝试使用substring string,但它只打印firstname =然后丢弃实际名称。

<form method="post" onsubmit="return savecookies()" autocomplete="on">
      <div class="sep">
        <h1 class="header">Welcome <span id="greetings"></span></h1>
      </div><br> Firstname
      <br>
      <input type="text" name="fname" id="fname" pattern="[A-Za-z\-]+" maxlength="25" required placeholder="Enter first name" autofocus><br> Lastname
      <br>
      <input type="text" name="lname" id="lname" pattern="[A-Za-z\-]+" maxlength="25" required placeholder="Enter last name"><br>
      <input type="submit" onclick="savecookies(); return false;" value="Submit">
      </div>
</form>

 function savecookies() {
      var fname = document.getElementById("fname").value;
      setCookie("firstname", fname, 1);

      var lname = document.getElementById("lname").value;
      setCookie("lastname", lname, 1);
    }

function setCookie(cookieName, cookieValue, length) {
  var date = new Date();
  date.setTime(date.getTime() + (length * 24 * 60 * 60 * 1000));
  var expires = "expires=" + date.toGMTString();
  document.cookie = cookieName + "=" + cookieValue + ";" + expires + ";path=/";
}

function getCookie() {
  var decodedCookie = decodeURIComponent(document.cookie);

  var ca = decodedCookie.split(';');
  var msg = "";
  for (var i = 0; i < ca.length; i++) {
    var c = ca[i];
  }
  var msg = ca[1] + " " + ca[2];
  document.getElementById("greetings").innerHTML = msg;
  return "";
}

1 个答案:

答案 0 :(得分:3)

请参阅Firefox和Safari中的这项工作

 <form >
      <div class="sep">
        <h1 class="header">Welcome <span id="firstname"></span> <span id="lastname"></span></h1>
      </div><br>


      <br>
      <input type="text" name="fname" id="fname" pattern="[A-Za-z\-]+" maxlength="25" required placeholder="Enter first name" autofocus><br> Lastname
      <br>
      <input type="text" name="lname" id="lname" pattern="[A-Za-z\-]+" maxlength="25" required placeholder="Enter last name"><br>
      <input type="button" onclick="savecookies();" value="save and show">
      </div>
</form>

<script>

 function savecookies() {

      var fname = document.getElementById("fname").value;
      setCookie("firstname", fname, 1);

      var lname = document.getElementById("lname").value;
      setCookie("lastname", lname, 1);

loadCookie();
}

function loadCookie()
{

  document.getElementById("firstname").innerHTML =  getCookie("firstname");
  document.getElementById("lastname").innerHTML =  getCookie("lastname");
}

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

function getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

</script>