Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing whitespace with a + character 1

Status
Not open for further replies.

SuperSal

Programmer
Mar 13, 2007
33
Hi everyone, I know this seems a really simple thing to do but I just can't get it working correctly. Basically the code is creating a URL CGI form submission. The code gathers all the form elements names and values and appends it to the pages URL. The problem is that I need to replace certain characters such as whitespace so that the server doesnt throw an error. The code is as below...can anyone help me out as to replace the whitespace with a + character. Also is there any other characters that I will need to be aware of.

function get_formdata(form_data)
{
var theForm = document.forms[0];
var textData = ""
var URL = location.href;

for(i=0; i<theForm.elements.length; i++){
if (theForm.elements.value != "")
{
if(theForm.elements.type == "text" || theForm.elements.type == "textarea" || theForm.elements.type == "button")
{
textData += theForm.elements.name + "=" + theForm.elements.value +'&'
}
else if(theForm.elements.type == "checkbox")
{
textData += theForm.elements.name + "=" + theForm.elements.checked +'&'
}
else if(theForm.elements.type == "select-one")
{
textData += theForm.elements.name + "=" + theForm.elements.options[theForm.elements.selectedIndex].text +'&'
}
}
}
var URL1 = URL+='?'+textData;
alert(URL+='?'+textData);

var CGIURL = URL1.replace('\s','+'); //this bit not working
alert(CGIURL);
}

Thanks
Sally
 
>var CGIURL = URL1.replace('\s','+'); //this bit not working
[tt]var CGIURL = URL1.replace(/\s/g,'+');[/tt]
 
Thanks tsuji.....YOU ARE A STAR!!!

Sally
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top