Useful notes & manuals
from the
world wide web
L:
P:
Register now!
Space in Url and JavaScript trim and replace function JavaScript
Solution from: http://codiebag.blogspot.com/2007/07/space-in-url-and-javascript-trim-and.html Created by tlw on 08.12.2019 @ 08:07

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");

trim   replace   string   url   space   %20
 
Created by THE LOST WEB © 2009-2012