Solved

Jascript issue: Need help replacing spaces with %20 in get parameters

Posted on 2013-05-21
12
281 Views
Last Modified: 2013-06-03
I'm having a very difficult time getting my javascript function to preserve (or convert) spaces when concatenating query string parameters for a search form:

Here is the form:

http://www.homerep.com/

And here is a link to the JS file:

http://www.homerep.com/wp-content/themes/twentyten/js/home_search.js

The problem that I'm having is that spaces are NOT being preserved when choosing a City from the select dropdown that's labeled "Search Any South Florida City".  For whatever reason, . the spaces are being completely stripped out.  

So when you choose - say - "Fort Lauderdale" from the dropdown, .. it results in the following incorrect query string parameter:

&Cities=FortLauderdale

... when it should be either "&Cities=Fort Lauderdale" or "&Cities=Fort%20Lauderdale"

I've tried using encodeURI(), .. encodeURIComponent(), and replace() javascript functions .. but none of them are working.  Seriously .. I've been tinkering with these functions for nearly 2 hours already, and I can't for the life of my figure out what's going on.

What do I need to do/change in my javascript to ensure that the spaces in the selected City Names are preserves with assembling the query string parameters?

Thanks,
- Yvan
0
Comment
Question by:egoselfaxis
  • 8
  • 3
12 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39184762
Change
+ queryString
to
+ encodeURIComponent(queryString)
0
 
LVL 15

Expert Comment

by:Jagadishwor Dulal
ID: 39184763
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39184773
So

FinalUrl = "/" + this.URL_Root + "&PerformSearch" + encodeURIComponent(queryString);                  

Also no need to navigate:

  window.location = FinalUrl;                  

can replace

if(typeof window.navigate != "undefined"){            
  window.navigate(FinalUrl);                        
} else {            
  location.href = FinalUrl;                  
}
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39184791
OOps. I re-read your code.

you need to change all

xxx.value to
encodeURIComponent(xxx.value)

so

(city == null || city.value == "" ? "" : "&Cities=" +  encodeURIComponent(city.value)  ) +
0
 

Author Comment

by:egoselfaxis
ID: 39184813
I've tried all of these suggestions already, and none of them are working for me.
Try it for yourself and you will see.

- yg
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39184872
in 30 minutes
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39184908
Before I test try


FinalUrl = "/" + this.URL_Root + "&PerformSearch" + queryString.replace(/ /g,"%20");
0
 

Author Comment

by:egoselfaxis
ID: 39184942
Nope .. that didn't work.

I left your code modification in place so you can test it for yourself:

http://www.homerep.com/wp-content/themes/twentyten/js/home_search.js

- yg
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 39185037
Actually my code works just fine

/HomeRep/Search?&PerformSearch&PropertyTypes=2678&Cities=Bal%20Harbour%20Islands

Your server then changes the URL to

http://www.homerep.com/search/?PerformSearch&PropertyTypes=2678&Cities=BalHarbourIslands
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39185047
See the redirect:

redirect
0
 

Author Comment

by:egoselfaxis
ID: 39185232
What makes you think that the server configuration is the culprit?  I ask because I am able to manually add in the spaces in the url after generating a search result, and no redirection occurs:

http://www.homerep.com/search/?PerformSearch&PropertyTypes=2678&Cities=Bal%20Harbour%20Islands

Did I mention that this is a Wordpress based site?  Would that have anything to do with it, perhaps?

 - yg
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39185338
Possibly. Perhaps there is some process that strips out spaces,
It is 100% surely something on the server because the spaced URL gets a 301 so a hard redirect
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question