Solved

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

Posted on 2013-05-21
12
282 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

726 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