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

06-22-2004, 06:40 AM

Here's the script:

function checkForm(){
if (document.myForm.cbox.checked){
alert('You Must Agree To The Terms To Continue');

Here's the form:

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

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!


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.


function checkForm(f){
if (f.cbox.checked){
return true; //continue form submission
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" />

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.

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.


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

onsubmit="return checkForm(this)">

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