157

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

asked Nov 27, 2010 at 17:27
3
  • 37
    Did you check encodeURIComponent and decodeURIComponent? Commented Nov 27, 2010 at 17:31
  • is that really the same thing? Commented Nov 27, 2010 at 17:35
  • @Gumbo: the same thing as URL encoding and URL decoding - blooberry.com/indexdot/html/topics/urlencoding.htm Commented Nov 27, 2010 at 17:46

9 Answers 9

239

Here is a complete function (taken from PHPJS):

function urldecode(str) {
 return decodeURIComponent((str+'').replace(/\+/g, '%20'));
}
Matt
75.5k26 gold badges156 silver badges181 bronze badges
answered Dec 16, 2010 at 8:03
Sign up to request clarification or add additional context in comments.

11 Comments

+1: this is the true urldecode, handles the case of space being encoded as +, thanks a lot
This is what I needed to use when the data was encoded with PHP's urlencode() function.
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?
@ChrisMoschini: No, this is correct because the replace occurs before the decode call.
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!!
|
220
TJL
6,3228 gold badges37 silver badges37 bronze badges
answered Nov 27, 2010 at 17:34

4 Comments

thanks guys! looks like the right answer, but Gumbo answered this first.. I feel like he deserves the credit
This is an incomplete answer. See the urlDecode() implementation below by @anshuman.
Nice but it didn't remove '+' signs. @anshuman's answer worked best for me
Doesn't work with spaces which are represented with ++++
9

Use this

unescape(str);

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

answered Feb 13, 2013 at 12:51

1 Comment

Cool answer, but looks like it's obsoleted in favor of decodeURIComponent() due to poor support of non-ASCII characters.
9
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]
answered Apr 19, 2011 at 18:12

1 Comment

You should use window.location.search instead.
3
//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));
Dennis Hackethal
14.4k12 gold badges75 silver badges129 bronze badges
answered Jan 11, 2013 at 6:40

1 Comment

Thank you, this worked for me. decodeURIComponent did not work for me (Malformed URI sequence).
2

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.

answered Apr 29, 2014 at 16:39

Comments

0

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

answered Jul 15, 2015 at 13:03

Comments

0

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

CertainPerformance
374k55 gold badges354 silver badges359 bronze badges
answered Aug 4, 2018 at 6:09

Comments

0

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!

answered May 31, 2020 at 20:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.