PDA

View Full Version : Replacing White space in variables



caspur
09-24-2009, 08:55 PM
I have a series of variables which ultimately form a hyperlink. During the document write process for the actual hyperlink if there are spaces found the string breaks and does not include any characters past the space. I am using a replace function on the input variable to accomodate for this and add "%20", however just found the way I wrote it, this is only replacing the first instance... Any thoughts out there on how to do this the right way to catch all spaces in the string?

<script language="javascript" type="text/javascript">
var path = "http://URL/";
var report = reportSelect.value;
var reportName = report.replace(/ /,"%20");
document.write(report + "<BR/>" + "<A href=" + path + reportName + ".xml" + " target=viewer>" + "Show Report" + "</A>")
</script>

techietim
09-24-2009, 09:24 PM
Just use escape()?

vwphillips
09-25-2009, 07:04 AM
report.replace(/ /g,"%20");

jscheuer1
09-26-2009, 02:43 AM
Both previous responses in this thread appear workable. However, to conform to present standards (giving you the best shot at future compatibility), one should employ the encodeURI function. For more info on its use, see:

http://www.w3schools.com/jsref/jsref_encodeURI.asp

or your favorite resource for info about built in javascript functions.