如何利用cookie中保存的URL?

时间:2017-02-22 11:59:28

标签: javascript angularjs cookies

我使用Angular

在Cookie中保存网址

$cookies.put("targetURL": "http://partners.api.xyz.net/apiservices/deeplink/v2?_cje=TmvlUQgTj%2fbxOQ%2bkXy5haJR9JWcHT9aaSjEP%2fEbwSq4uZqOYt%2fRV9FYsU1PT80PD&url=http%3a%2f%2fwww.apideeplink.com%26cabin_class%3deconomy%26facilitated%3dfalse%26ticket_price%3d72.82%26is_npt%3dfalse%26is_multipart%3dfalse%26client_id%3d2017-02-22T10%3a02%3a07")

但是,我必须使用普通的javascript检索此cookie。当我尝试获取它时,字符串将转换为

http%3A%2F%2Fpartners.api.xyz.net%2Fapiservices%2Fdeeplink%2Fv2%3F_cje%3DTmvlUQgTj%252fbxOQ%252bkXy5haJR9JWcHT9aaSjEP%252fEbwSq4uZqOYt%252fRV9FYsU1PT80PD%26url%3Dhttp%253a%252f%252fwww.apideeplink.com%2526cabin_class%253deconomy%2526facilitated%253dfalse%2…e%2526is_multipart%253dfalse%2526client_id%253d2017-02-22T10%253a02%253a07”,不再可点击了。

要检索我正在使用该功能的cookie:

function getCookie(name) {
  var value = "; " + document.cookie;
  var parts = value.split("; " + name + "=");
  if (parts.length == 2) return parts.pop().split(";").shift();
}

为什么字符串会像这样改变,我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:2)

您需要使用decodeURIComponent()

解码URI
old = "http%3A%2F%2F";
new = decodeURIComponent( old );

#new => http://