0

I'm wanting to get data from the URL query string and display it in the body of an HTML document. I can't get the script to replace the + signs with an empty space.

Example URL: www.mywebsite.com/?item=Sandwich&bread=White+Bread

In the HTML body, the text field for item would show "Sandwich" but the text field for bread would show "White+Bread." How can I replace the + with a space?

Here's the function Im using to get value from the URL

function querystring(key) {
    var re=new RegExp('(?:\\?|&)'+key+'=(.*?)(?=&|$)','gi');
    var r=[], m;
    while ((m=re.exec(document.location.search)) != null) r.push(m[1]);
    return r;
}

    $("span.item").text(querystring('item'));
    $("span.bread").text(querystring('bread'));

I've tried using this but it's telling me arrays don't have a replace function.

.replace(/\+/g, ' ')
Allan Kimmer Jensen
  • 4,333
  • 2
  • 31
  • 53
Narong
  • 476
  • 1
  • 3
  • 15
  • 2
    That's because arrays don't have a replace method. Where did you try to use that `.replace()` call? If you put it inside of the `while` loop (like `r.push(m[1].replace(...))`, it might work. Also, why aren't you using one of the many user-created functions that do this parsing already? There are some that optimize the retrieval of a key's value from the querystring, as well as much better support for things like this. For example - http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript (I like the second answer) – Ian Feb 11 '14 at 17:30
  • maybe I did not understand the question correctly but why not use `encodeURI() / decodeURI()` ? – blurfus Feb 11 '14 at 17:35
  • Thanks, Allan. I tried searching, but I guess I wasn't searching for the right tags. That solution is much more efficient and works perfect for me. – Narong Feb 11 '14 at 17:50

2 Answers2

3

Your function returns an array, try changing it by this:

function querystring(key) {
    var re=new RegExp('(?:\\?|&)'+key+'=(.*?)(?=&|$)','gi');
    var r=[], m;
    while ((m=re.exec(document.location.search)) != null) r.push(m[1]);
    return r[0];
}

then you can use your replace

.replace(/\+/g, ' ')
Andy
  • 61,948
  • 13
  • 68
  • 95
Emilio Rodriguez
  • 5,709
  • 4
  • 27
  • 32
  • This is a valid fix for my problem, but please refer to Allan Kimmer Jensen's comment under my question for a better solution. Thanks! – Narong Feb 11 '14 at 17:49
0

I'd replace your function with something like this, getting rid of the regular expression for the sake of readability. It handles undefined keys/values, plus you could easily refactor the first part of it into a parseQueryString function returning a dictionary for the current document.location.search.

function querystring(key) {
   var hash = {};

   var pairs = document.location.search.substr(1).split('&');
   for(var i = 0; i < pairs.length; i++) {
       var tuple = pairs[i].split('=');
       hash[tuple[0]] = tuple[1];
   }

   var val = hash[key];
   return (val) ? val.replace(/\+/g, ' ') : undefined;
}

Usage:

querystring('bread');
> White Bread
marionebl
  • 3,342
  • 20
  • 34