Passing string through URL

I have a query string that I’m trying to pass onto another page. When I enter the url string into a browser, it works fine, the data is passed. However, when trying to formulate the string using Java Script, it does not pass. Could you please check this line of code and let me know if it looks ok.

The Javascript code I’m using:

window.location = 'http://www.domain.com/test.asp?FirstName=' + escape(document.getElementById('FirstName').value);

The url that needs to be produced that works if I put in manually into the browser:

http://www.domain.com/test.asp?FirstName=John

A bit of background info on the java script purpose. I am using java script to create and validate a form, rather than using form tags on my main page. The rest of the javascript code is simple and should not be the cause. Just in case, I’ve included the code minus the extra form fields.

function ValidateForm() {
var isFormValid = 1;
var errorMessage = '';
var firstName = document.getElementById('FirstName');
if(firstName.value.length < 1) 
{
 isFormValid = 0;
 errorMessage = errorMessage + '- First Name is required.\
';
}
if(isFormValid == 0)
{
 alert(errorMessage);
}
else
{
 // Send the data and user to next step
  window.location = 'http://www.domain.com/test.asp?FirstName=' + escape(document.getElementById('FirstName').value);
}
}
 
 
document.write('<table width="225" border="0" cellspacing="0" cellpadding="3" align="center" class="newform">');
document.write('<tr>');
document.write('<td height="50" width="60"></td>');
document.write('<td height="50" width="140"></td>');
document.write('</tr>');
document.write('<tr>');
document.write('<td height="28" align="right" nowrap="nowrap">First Name</td>');
document.write('<td><input name="FirstName" id="FirstName" type="text" size="20"></td>');
document.write('</tr>');
document.write('<tr>');
document.write('<td></td>');
document.write('<td><a href="http://www.domain.com/ThankYou.asp"><input name="SubmitButton" id="SubmitButton" type="image" onClick="return ValidateForm();" value="submit" src="images/newbutton.gif"></a>');
document.write('</td>');
document.write('</tr>');
document.write('</table>');
document.all('FirstName').focus();

What URL does it pass?

so where does your dynamically generated lnk take you then? What url does it print? Or does it just miss the first name param off?

I’d try assign the variable before (like you have here: var firstName = document.getElementById(‘FirstName’);
)

and then do:

window.location = 'http://www.domain.com/test.asp?FirstName=' + escape(firstname);

Double check the value of ‘firstname’ by alerting it out to make sure the info is getting in there

The url is http://www.domain.com/test.asp. i need to pass the FirstName to that URL using Javascript.

The test.asp page connects to a database to post the data there.

The link has an onclick attribute that returns the result from ValidateForm()
When false is returned, the default behaviour for the link element is prevented.
When anything else is returned, the link does what it normally does.

You need to return false from the ValidateForm() function to stop the default link from being followed. Once you’ve done that the window.location will be followed instead.

Thank you, I’ll try that and see how it works. Orginally I was trying to send data using the window.location but send the user to the other link in the code.

I got it to work to a certian extent. The problem was on the test page and not in the Java Script. However I encountered a new problem. The window.location does not work in IE, but does in FireFox. I also tried using window.location.href, but that did not work. Is window.location buggy in IE?

window.location isn’t buggy, it’ll be how other parts of the script are handled, such as the default action of elements that events have been fired upon.