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));
Related videos on Youtube
Author by
at.
Updated on June 01, 2020Comments
-
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 over 13 yearsDid you check
encodeURIComponent
anddecodeURIComponent
? -
at. over 13 yearsis that really the same thing?
-
at. over 13 years@Gumbo: the same thing as URL encoding and URL decoding - blooberry.com/indexdot/html/topics/urlencoding.htm
-
-
at. over 13 yearsthanks guys! looks like the right answer, but Gumbo answered this first.. I feel like he deserves the credit
-
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 over 12 yearsThis is an incomplete answer. See the urlDecode() implementation below by @anshuman.
-
Scott Keck-Warren about 12 yearsThis is what I needed to use when the data was encoded with PHP's urlencode() function.
-
MusikAnimal over 11 yearsNice but it didn't remove '+' signs. @anshuman's answer worked best for me
-
Chris Moschini about 11 yearsIf 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 about 11 years@ChrisMoschini: No, this is correct because the replace occurs before the decode call.
-
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 about 11 yearsThank 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 almost 11 yearsCool answer, but looks like it's obsoleted in favor of
decodeURIComponent()
due to poor support of non-ASCII characters. -
VoidKing over 10 yearsThis method is also better if the string was encoded with
HttpServerUtility.UrlEncode
(which encodes spaces as "+"s) instead ofHttpServerUtility.UrlPathEncode
(which encodes spaces as "%20") in C#.NET, as well. -
Octopus over 9 yearsif, 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 about 9 yearsDoesn't work with spaces which are represented with ++++
-
mjs about 9 yearsAre the plus signes the only thing that needs to be replaced ?
-
anshuman over 8 years@momo Other things taken care by decodeURIComponent.
-
Smile4ever about 8 yearsThank you, this worked for me. decodeURIComponent did not work for me (Malformed URI sequence).
-
aloisdg over 7 yearsYou should use
window.location.search
instead. -
TheRealFakeNews over 7 years@anshuman What if I need to decode what I encoded using
urldecode
? WillencodeURIComponent
work just fine or will I need to alter the input as you did withurldecode
?