In a recent (also age-old) mapping application for our website (http://www.mcic.org), I encountered 2 problems: 1. how to trim the leading and trailing space in the url's parameter; 2. how to include space in an url
(I must have been living in rocks that I never realized that it was illegal to include flat space in url, and the right way to do it is to replace ALL spaces with %20.)
There is no built in trim() function in Javascript, fortunately there are many people who has nothing better to do than write code and present it online. So here is a prefect trim function :
string=string.replace(/^s+|s+$/g, '');
Cever use of regular expressions.
As for replacing all spaces inside a string with %20, here it goes
string=string.replace(/s/g,"%20");
|