0

referrer` and saving the value in a cookie. The result is coming like this with strange characters:

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

How I can remove those strange characters so the link will show correctly please?

thanks

Rene Zammit
  • 1,121
  • 5
  • 19
  • 42

1 Answers1

3
  1. use a cookie script that unescapes the cookie before returning it OR
  2. look here how to urldecode: Javascript equivalent to php's urldecode()
var url = "http%3A//www.rzammit.com/props/testpage.asp%3Fadv%3D123%26loc%3D45";
url = decodeURIComponent(url.replace(/\+/g, ' '));

Here is the cookiescript I have used since mid '90s - fee free to replace escape with encodeURIComponent and unescape with decodeURIComponent to bring it into the 2010s ;)

// 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";
}
Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • 1
    It's not `document.referrer` he's working with directly, but the value after being passed through a cookie. `document.referrer` isn't URL encoded. – Chris Morgan Apr 19 '12 at 12:01