Solved

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

Posted on 2013-05-21
12
276 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
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…
The viewer will learn how to dynamically set the form action using jQuery.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now