JavaScript URL Decode function

JavascriptJqueryUrlencodeUrldecode

Javascript Problem Overview


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

Javascript Solutions


Solution 1 - Javascript

Here is a complete function (taken from PHPJS):

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

Solution 2 - Javascript

Solution 3 - Javascript

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 - Javascript

Use this

unescape(str);

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

Solution 5 - Javascript

//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));

Solution 6 - Javascript

If you are responsible for encoding the data in PHP using urlencode, PHP's rawurlencode works with JavaScript's decodeURIComponent without needing to replace the + character.

Solution 7 - Javascript

Here's what I used:

In JavaScript:

var url = "http://www.mynewsfeed.com/articles/index.php?id=17";
var encoded_url = encodeURIComponent(url);
	
var decoded_url = decodeURIComponent(encoded_url);

In PHP:

$url = "http://www.mynewsfeed.com/articles/index.php?id=17";
$encoded_url = url_encode(url);
	
$decoded_url = url_decode($encoded_url);

You can also try it online here: http://www.mynewsfeed.x10.mx/articles/index.php?id=17

Solution 8 - Javascript

var uri = "my test.asp?name=ståle&car=saab";
console.log(encodeURI(uri));

Solution 9 - Javascript

decodeURIComponent() is fine, but you never want ot use encodeURIComponent() directly. This fails to escape reserved characters like *, !, ', (, and ). Check out RFC3986, where this is defined, for more info on that. The Mozilla Developer Network documentation gives both a good explanation and a solution. Explanation...

>To be more stringent in adhering to RFC 3986 (which reserves !, ', (, ), and *), even though these characters have no formalized URI delimiting uses, the following can be safely used:

Solution...

function fixedEncodeURIComponent(str) {
  return encodeURIComponent(str).replace(/[!'()*]/g, function(c) {
    return '%' + c.charCodeAt(0).toString(16);
  });
}

In case you're not sure, check out a good, working demo at JSBin.com. Compare this with a bad, working demo at JSBin.com using encodeURIComponent() directly.

Good code results:

> thing%2athing%20thing%21

Bad code results from encodeURIComponent():

> thing*thing%20thing!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionat.View Question on Stackoverflow
Solution 1 - JavascriptanshumanView Answer on Stackoverflow
Solution 2 - JavascriptGeoffView Answer on Stackoverflow
Solution 3 - JavascriptEtienne DupuisView Answer on Stackoverflow
Solution 4 - JavascriptnithinreddyView Answer on Stackoverflow
Solution 5 - JavascriptIrfan MView Answer on Stackoverflow
Solution 6 - JavascriptBrent SelfView Answer on Stackoverflow
Solution 7 - Javascriptuser3572058View Answer on Stackoverflow
Solution 8 - Javascriptmethodpool.ioView Answer on Stackoverflow
Solution 9 - JavascriptHoldOffHungerView Answer on Stackoverflow