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, ' ')
3
  • 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 - stackoverflow.com/questions/901115/… (I like the second answer) Commented Feb 11, 2014 at 17:30
  • maybe I did not understand the question correctly but why not use encodeURI() / decodeURI() ? Commented Feb 11, 2014 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. Commented Feb 11, 2014 at 17:50

2 Answers 2

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, ' ')
Sign up to request clarification or add additional context in comments.

1 Comment

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

Comments

Your Answer

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.