Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts

    "window.top.location.href" statement won't execute...

    Hi:

    Here's the script:


    Code:
    <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:

    Code:
    <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
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    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:
    Code:
    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.

  • #3
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Sorry, I forgot the return in the onsubmit handler.

    onsubmit="return checkForm(this)">

  • #5
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, Glenn...That was it!


    Appreciated!


    -james
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •