如何从cookie

时间:2017-03-23 10:28:33

标签: javascript

以下是我用于设置浏览器Cookie的一段代码我遇到的问题是我无法移除"%20"并用空格代替。每当我在变量值中有空格时,它们都被%20替换,我不知道为什么以及如何纠正这个问题。任何建议都非常感谢。

  var today = new Date();  
  var expiry = new Date(today.getTime() + 30 * 24 * 3600 * 1000); // plus 30 days

  function setCookie(name, value){
    document.cookie=name + "=" + escape(value) + "; path=/; expires=" + expiry.toGMTString();
  }

function putCookie(form){
   setCookie("FirstName", form[0].FirstName.value) = FirstName.replace(/%20/g," ");  
   setCookie("LastName", form[0].LastName.value) = LastName.replace(/%20/g," ");

   return true;
  }

2 个答案:

答案 0 :(得分:4)

看看decodeURIComponent

decodeURIComponent('some%20val') // => 'some val'

答案 1 :(得分:0)

setCookie("FirstName", decodeURIComponent(form[0].FirstName.value));
setCookie("LastName", decodeURIComponent(form[0].LastName.value));

结帐工作演示https://jsfiddle.net/pvmzqrye/