0

referrer` 并将值保存在 cookie 中。结果是这样的,带有奇怪的字符:

http%3A//www.rzammit.com/props/testpage.asp%3Fadv%3D123%26loc%3D45

如何删除那些奇怪的字符,以便链接正确显示?

谢谢

4

1 回答 1

2
  1. 使用 cookie 脚本在返回 cookie 之前将其取消转义或
  2. 看这里如何 urldecode:Javascript 等价于 php 的 urldecode()
var url = "http%3A//www.rzammit.com/props/testpage.asp%3Fadv%3D123%26loc%3D45";
url = decodeURIComponent(url.replace(/\+/g, ' '));

这是我自 90 年代中期以来一直使用的 cookiescript - 免费将 escape 替换为 encodeURIComponent 和 unescape 替换为 decodeURIComponent 以将其带入 2010 年代;)

// cookie.js file
var cookieToday = new Date(); 
var expiryDate = new Date(cookieToday.getTime() + (365 * 86400000)); // a year

/* Cookie functions originally by Bill Dortsch */

function setCookie (name,value,expires,path,theDomain,secure) { 
   value = escape(value);
   var theCookie = name + "=" + value + 
   ((expires)    ? "; expires=" + expires.toGMTString() : "") + 
   ((path)       ? "; path="    + path   : "") + 
   ((theDomain)  ? "; domain="  + theDomain : "") + 
   ((secure)     ? "; secure"            : ""); 
   document.cookie = theCookie;
} 

function getCookie(Name) { 
   var search = Name + "=" 
   if (document.cookie.length > 0) { // if there are any cookies 
      var offset = document.cookie.indexOf(search) 
      if (offset != -1) { // if cookie exists 
         offset += search.length 
         // set index of beginning of value 
         var end = document.cookie.indexOf(";", offset) 
         // set index of end of cookie value 
         if (end == -1) end = document.cookie.length 
         return unescape(document.cookie.substring(offset, end)) 
      } 
   } 
} 
function delCookie(name,path,domain) {
   if (getCookie(name)) document.cookie = name + "=" +
      ((path)   ? ";path="   + path   : "") +
      ((domain) ? ";domain=" + domain : "") +
      ";expires=Thu, 01-Jan-70 00:00:01 GMT";
}
于 2012-04-19T11:59:27.797 回答