Solved

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

Posted on 2013-05-21
12
284 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

705 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