...

View Full Version : "window.top.location.href" statement won't execute...



jamescover
06-22-2004, 06:40 AM
Hi:

Here's the script:




<script>
<!--
function checkForm(){
if (document.myForm.cbox.checked){
window.top.location.href="http://yourDomain.com/yourPage.php";
}else{
alert('You Must Agree To The Terms To Continue');
}
}
//-->
</script>




Here's the form:



<form name=myForm onsubmit=checkForm() method=post>
<input type=checkbox name=cbox checked>
<input type=submit value=Agree>
</form>


Does anyone know why the first statement (window.top.location.href="http://yourDomain.com/yourPage.php" ; ) won't execute?

If I alert the conditional (alert (document.myForm.cbox.checked) ), it comes back true.

If I call the script from an anchor tag (<a href=javascript:checkForm()>Check Form</a>) it executes.

If I use a method, like alert(), or open(), after the conditional, they execute.

???

Any ideas appreciated. Thanks!


-james

glenngv
06-22-2004, 06:49 AM
Because you're using a submit button, the form submits. And since there is no action attribute specified in the form, it submits to itself.

Solution:


function checkForm(f){
if (f.cbox.checked){
return true; //continue form submission
}
else{
alert('You Must Agree To The Terms To Continue');
return false; //cancel form submission
}
}
...
<form name="myForm" action="http://yourDomain.com/yourPage.php" method="post" onsubmit="checkForm(this)">
<input type="checkbox" name="cbox" />
<input type="submit" value="Agree" />
</form>

I defaulted the checkbox to unchecked so that the user is forced to check the checkbox to agree.

If you are using frames (as I noticed you used window.top) and you want to submit the page to the outermost window, then you need to add target="_top" to the <form> tag. And you also need to validate again in the php page so that users with javascript disabled will not pass through the page.

jamescover
06-22-2004, 07:09 AM
Hi glenngv:

Thanks for your response!!!

A small problem--using your code, the form still submits if checked is false, i.e., after alert confirmation.


-james

glenngv
06-22-2004, 07:18 AM
Sorry, I forgot the return in the onsubmit handler.

onsubmit="return checkForm(this)">

jamescover
06-22-2004, 07:30 AM
Thanks, Glenn...That was it!


Appreciated!


-james



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum