JavaScript URL Decode function

195,931

Solution 1

I've used encodeURIComponent() and decodeURIComponent() too.

Solution 2

Here is a complete function (taken from PHPJS):

function urldecode(str) {
   return decodeURIComponent((str+'').replace(/\+/g, '%20'));
}

Solution 3

decodeURIComponent(mystring);

you can get passed parameters by using this bit of code:

//parse URL to get values: var i = getUrlVars()["i"];
function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

Or this one-liner to get the parameters:

location.search.split("your_parameter=")[1]

Solution 4

Use this

unescape(str);

I'm not a great JS programmer, tried all, and this worked awesome!

Solution 5

//How decodeURIComponent Works

function proURIDecoder(val)
{
  val=val.replace(/\+/g, '%20');
  var str=val.split("%");
  var cval=str[0];
  for (var i=1;i<str.length;i++)
  {
    cval+=String.fromCharCode(parseInt(str[i].substring(0,2),16))+str[i].substring(2);
  }

  return cval;
}

document.write(proURIDecoder(window.location.href));
Share:
195,931

Related videos on Youtube

at.
Author by

at.

Updated on June 01, 2020

Comments

  • at.
    at. almost 4 years

    What's the best JavaScript URL decode utility? Encoding would be nice too and working well with jQuery is an added bonus.

    • Gumbo
      Gumbo over 13 years
      Did you check encodeURIComponent and decodeURIComponent?
    • at.
      at. over 13 years
      is that really the same thing?
    • at.
      at. over 13 years
      @Gumbo: the same thing as URL encoding and URL decoding - blooberry.com/indexdot/html/topics/urlencoding.htm
  • at.
    at. over 13 years
    thanks guys! looks like the right answer, but Gumbo answered this first.. I feel like he deserves the credit
  • Máthé Endre-Botond
    Máthé Endre-Botond over 12 years
    +1: this is the true urldecode, handles the case of space being encoded as +, thanks a lot
  • Steven Francolla
    Steven Francolla over 12 years
    This is an incomplete answer. See the urlDecode() implementation below by @anshuman.
  • Scott Keck-Warren
    Scott Keck-Warren about 12 years
    This is what I needed to use when the data was encoded with PHP's urlencode() function.
  • MusikAnimal
    MusikAnimal over 11 years
    Nice but it didn't remove '+' signs. @anshuman's answer worked best for me
  • Chris Moschini
    Chris Moschini about 11 years
    If I'm not mistaken, this is decoding + as %20, not as space - that's not really what you wanted here, is it? You'd want the space, not another version of an encoded character... no?
  • Elyas Behroozizade
    Elyas Behroozizade about 11 years
    @ChrisMoschini: No, this is correct because the replace occurs before the decode call.
  • Chris Moschini
    Chris Moschini about 11 years
    @lfaraone I see - so the goal of that tidbit of code is to ensure plus signs come out as spaces in the output instead of being left be as plusses by decodeURIComponent.
  • Dante Cullari
    Dante Cullari about 11 years
    Thank you so much!! This is a perfect way to handle strings in jquery which have been previously urlencoded with php, just what I needed!!
  • Brad Koch
    Brad Koch almost 11 years
    Cool answer, but looks like it's obsoleted in favor of decodeURIComponent() due to poor support of non-ASCII characters.
  • VoidKing
    VoidKing over 10 years
    This method is also better if the string was encoded with HttpServerUtility.UrlEncode (which encodes spaces as "+"s) instead of HttpServerUtility.UrlPathEncode (which encodes spaces as "%20") in C#.NET, as well.
  • Octopus
    Octopus over 9 years
    if, in PHP, you json_encode() a value and then put that in a cookie with setcookie(), you may need to do this before you JSON.parse() it. even though you didnt explicitly urlencode() it.
  • mjs
    mjs about 9 years
    Doesn't work with spaces which are represented with ++++
  • mjs
    mjs about 9 years
    Are the plus signes the only thing that needs to be replaced ?
  • anshuman
    anshuman over 8 years
    @momo Other things taken care by decodeURIComponent.
  • Smile4ever
    Smile4ever about 8 years
    Thank you, this worked for me. decodeURIComponent did not work for me (Malformed URI sequence).
  • aloisdg
    aloisdg over 7 years
    You should use window.location.search instead.
  • TheRealFakeNews
    TheRealFakeNews over 7 years
    @anshuman What if I need to decode what I encoded using urldecode? Will encodeURIComponent work just fine or will I need to alter the input as you did with urldecode?