2017-03-06 49 views
2

我試圖打印我已經存儲的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 ""; 
} 
+0

這項工作,是因爲你打印存儲在瀏覽器中的所有Cookie。嘗試通過名稱調用cookie並打印它。 – CodeMonkey

回答

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> 
+0

這是正確的答案,但嘗試實現一個函數來接受多個參數來獲取必要的cookie。 – CodeMonkey

+0

看到了,我更新了。有了2個元素,會是這樣嗎? –

+0

這符合他的需求。我正在考慮給多個參數接受提供給'getCookie(cname)'函數的任何類型的參數。所以他會有一個動態選項來獲取cookie。無論如何,這將是太高級。這是他需要的。歡呼的支持。 – CodeMonkey