Prevent Premature clicking on a submit button while page is refreshed

Discussion in 'Javascript' started by anonieko, Aug 14, 2005.

  1. anonieko

    anonieko Guest

    Scenario: You have a page that is TOO slow to refresh.
    But it allows partial flushing of html contents. I.e.
    Submit button already appears but you don't
    want your users to click on it prematurely
    because other parts are still coming.


    Here I put a javascript the will enable only
    submit button only after 5 seconds after the page
    is load fully.

    The prevention of button being clicked twice
    is also done .


    <html>
    <head>
    </head>
    <body>

    <form id="testForm" >
    <input id="test" type=text>
    <input type=button value="1click only" onclick="dosubmit()">
    </form>



    <script type="text/javascript">
    <!--
    var submitted = false;
    var timer1=setTimeout('EnableClick()', 5000);

    function EnableClick() {
    if (typeof submitted == 'undefined') return false;
    submitted = false;
    }

    function dosubmit(method) {
    // prevent clicking twice
    if (typeof submitted == 'undefined') return false;
    if ( submitted ) return false;
    submitted = true;
    clearTimeout(timer1);
    document.testForm.submit();
    }
    -->
    </script>


    </body
     
    anonieko, Aug 14, 2005
    #1
    1. Advertisements

  2. anonieko

    RobG Guest

    How about using script to prevent submission say by disabling the submit
    button. Then have an onload function that enables it.

    Your method makes submission of the form dependent on JavaScript, which
    it should not be, and makes users wait some arbitrary time that you
    think might let the page load fully. Why 5 seconds? Some users will
    have to wait longer than necessary, others will likely still not have
    the form fully loaded.
    From the script below, you attempt to wait for 5 seconds after the
    script is loaded, not when the page has finished loading. Your script
    does not prevent submission anyway.
    That should be handled at the server, not the client, and your script
    does not guarantee only one submit.
    HTML comments in script elements are completely unnecessary and possibly
    harmful, don't use them.
    'submitted' is defined as 'false', this test will always return false.
    'submitted' is never undefined, this test will always return false.
    This method of accessing form elements fails in Firefox, you need to
    either give the form a name (testForm would be good) or use:

    document.forms['testForm'].submit();

    [...]
     
    RobG, Aug 15, 2005
    #2
    1. Advertisements

  3. anonieko

    anonieko Guest

    That is exactly the opinion I wanted! Great!
     
    anonieko, Aug 15, 2005
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.