Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams @Gumbo: the same thing as URL encoding and URL decoding - blooberry.com/indexdot/html/topics/urlencoding.htm at. Nov 27, 2010 at 17:46 +1: this is the true urldecode, handles the case of space being encoded as +, thanks a lot Máthé Endre-Botond Oct 30, 2011 at 14:13 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? Chris Moschini Mar 1, 2013 at 9:09 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!! Dante Cullari May 3, 2013 at 20:32 thanks guys! looks like the right answer, but Gumbo answered this first.. I feel like he deserves the credit at. Nov 27, 2010 at 18:03 Cool answer, but looks like it's obsoleted in favor of decodeURIComponent() due to poor support of non-ASCII characters. Brad Koch Jun 6, 2013 at 14:35

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]
  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));
                Thank you, this worked for me. decodeURIComponent did not work for me (Malformed URI sequence).
– Smile4ever
                Apr 3, 2016 at 14:28
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

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!

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.