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 13 of 13
  1. #1
    New Coder
    Join Date
    Jun 2005
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript stop form submit after pressed

    Is there a way to stop a form submit after hitting submit? Refresh wouldn't realy be an option. Is abort() what I'm looking for?

  • #2
    New to the CF scene
    Join Date
    May 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this javascript:

    <SCRIPT LANGUAGE="JavaScript" TYPE="text/JavaScript">

    function formControl(submitted)
    {
    if(submitted=="1")
    {
    commentForm.Submit.disabled=true
    alert("Thanks for your submission!")
    }
    }

    </SCRIPT>
    Then add onClick="formControl(1)" to the appropriate input tag.

  • #3
    New Coder
    Join Date
    Jun 2005
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the response but that isn't what I'm looking for. Originally, I was just refreshing the page, but now I am adding this to a page that already has form results and that wont work anymore. Now I need to find a javascript equivalent to the "Stop" button in the browser.

  • #4
    New Coder
    Join Date
    Jun 2005
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here's what I've found so far:

    IE: use document.execCommand('Stop');
    Netscape/Mozilla/Firefox: use window.stop();

    I also read that Safari doesn't use either. Any way to get the same result with Safari?

  • #5
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    You would "return false" with your onsubmit call.

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,465
    Thanks
    0
    Thanked 634 Times in 624 Posts
    The last chance to stop the submit is t0 "return false" from the onsubmit event as after that the form has been submitted (even if it takes a while for the next page to show).
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if you want not to submit the form when pressing Submit button you can add to the button onclick="return false" for mouse clicks and onkeypress="return false" for keypress, as Enter or Spacebar.
    Of course you can also define some function instead of returning false to evaluate some condition dinamically.
    You can add also the onkeypress="return false" to any input field if you don't want the Enter to have a submit effect in the form.
    This works both on MF and IE

  • #8
    New to the CF scene
    Join Date
    Aug 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Stoping Browser request using javascript

    Please use below code for cross browsers solution.
    I tried it with FF3.5.2, Safari 3.1 and IE 7 and 8 its working fine

    if(navigator.appName == "Microsoft Internet Explorer")
    {
    window.document.execCommand('Stop');
    }
    else
    {
    window.stop();
    }

    Note: This will only stop the request form the browser like abort of AJAX(XMLHttpRequest).

  • #9
    Regular Coder
    Join Date
    Aug 2008
    Posts
    104
    Thanks
    4
    Thanked 14 Times in 14 Posts
    Why has this got so complicated? It's just return false; in whatever control you're using...

  • #10
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Look to MooTools

    I stumbled upon this thread because I have recently switched to jQuery. When writing an event function in MooTools, you can simple call evnt.stop(). jQuery does not implement such a function.

    Here is the solution that I extracted from MooTools. Because it is taken directly from the MooTools source code, I trust that it is cross-compatible:
    Code:
    function stopEvent(e) {
        if (e.stopPropagation) e.stopPropagation();
        else e.cancelBubble = true;
    
        if (e.preventDefault) e.preventDefault();
        else e.returnValue = false;
    }
    In jQuery, you can utilize this function like so:
    Code:
    $('#emailform').submit(function(e) {
            stopEvent(e);
    I suspect that with basic JavaScript, you can still use a variable in the function definition and it represents the event, but I'm not totally positive:
    Code:
    <form onsubmit="doSomething" action="..." >
    ...
    function doSomething(e) {
        stopEvent(e);
    
        //Your custom event function
    }
    By the way... you're all wrong! Doing "return false" will only get you so far. If you want to re-write the submit event function, you will need to do something like this.

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by n00ge View Post
    Is there a way to stop a form submit after hitting submit? Refresh wouldn't realy be an option. Is abort() what I'm looking for?
    What is the point of this? Why not simply disable the submit button? Or not even have a submit button. This quotation seems very apposite:-

    The Moving Finger writes; and, having writ,
    Moves on: nor all your piety nor wit
    Shall lure it back to cancel half a line,
    Nor all your tears wash out a word of it.

    -- Omar Khayyam

  • #12
    New Coder
    Join Date
    Apr 2007
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <form name="test" onsubmit="return test()">
    .
    .
    .
    .
    <input type="submit" value="Testing"></input>
    </form>

    Js :

    test()
    {
    return false;
    }

  • #13
    New Coder
    Join Date
    Apr 2007
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    @Philip :

    At times its necessary when you want to bypass the normal form submit, with an ajax submit. I use this regularly for the same purpose.

    Best,

    --
    Ajith


  •  

    Posting Permissions

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