如何从javascript中获取的cookie中删除%20和%40

时间:2016-05-25 16:18:05

标签: javascript jquery html css cookies

我正在拉一个cookie用于显示,但cookie显示%20和%40。如何在我的jquery或html / css中删除它?

function readCookie(FullName) {
   var name = FullName + "=";
   var ca = document.cookie.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 "";
}

HTML:

<label id="first">Cookie result: <input id="name2" maxlength="25" name="namefield" readonly="readonly" size="20" type="hidden" /> </label>
<script>
$("#first").append(document.getElementById('name2').value = readCookie('FullName') + "!");
</script>

2 个答案:

答案 0 :(得分:0)

我将.replace(/%20 / g,'')添加到readCookie('FullName')的末尾。它删除了%20

答案 1 :(得分:0)

使用

decodeURIComponent(document.cookie)

更多详细信息可以在文档中找到:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent