我試圖打印我已經存儲的cookie。例如,如果我進入約翰·史密斯那麼就應該是這樣的:如何使用cookie刪除某些字符?
Welcome John Smith
然而,當它打印它看起來像這樣:
Welcome firstname=John lastname=Smith
我不知道如何擺脫cookiename和標誌=
。我試圖使用substring字符串,但它只打印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 "";
}
這項工作,是因爲你打印存儲在瀏覽器中的所有Cookie。嘗試通過名稱調用cookie並打印它。 – CodeMonkey