Link to home
Create AccountLog in
Avatar of egoselfaxis

asked on

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

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:

And here is a link to the JS file:

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:


... 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?

- Yvan
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

+ queryString
+ encodeURIComponent(queryString)

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

Also no need to navigate:

  window.location = FinalUrl;                  

can replace

if(typeof window.navigate != "undefined"){            
} else {            
  location.href = FinalUrl;                  
OOps. I re-read your code.

you need to change all

xxx.value to


(city == null || city.value == "" ? "" : "&Cities=" +  encodeURIComponent(city.value)  ) +
Avatar of egoselfaxis


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
in 30 minutes
Before I test try

FinalUrl = "/" + this.URL_Root + "&PerformSearch" + queryString.replace(/ /g,"%20");
Nope .. that didn't work.

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

- yg
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
See the redirect:

User generated image
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:

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

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