• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

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:

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
egoselfaxis
Asked:
egoselfaxis
  • 8
  • 3
1 Solution
 
Michel PlungjanIT ExpertCommented:
Change
+ queryString
to
+ encodeURIComponent(queryString)
0
 
Jagadishwor DulalBraces MediaCommented:
0
 
Michel PlungjanIT ExpertCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Michel PlungjanIT ExpertCommented:
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
 
egoselfaxisAuthor Commented:
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
 
Michel PlungjanIT ExpertCommented:
in 30 minutes
0
 
Michel PlungjanIT ExpertCommented:
Before I test try


FinalUrl = "/" + this.URL_Root + "&PerformSearch" + queryString.replace(/ /g,"%20");
0
 
egoselfaxisAuthor Commented:
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
 
Michel PlungjanIT ExpertCommented:
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
 
Michel PlungjanIT ExpertCommented:
See the redirect:

redirect
0
 
egoselfaxisAuthor Commented:
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
 
Michel PlungjanIT ExpertCommented:
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now