View Full Version : Javascript stop form submit after pressed

05-02-2006, 03:36 PM
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?

05-02-2006, 04:03 PM
Try this javascript:

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

function formControl(submitted)
alert("Thanks for your submission!")

Then add onClick="formControl(1)" to the appropriate input tag.

05-02-2006, 04:13 PM
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.

05-02-2006, 04:29 PM
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?

05-02-2006, 06:35 PM
You would "return false" with your onsubmit call.

05-03-2006, 12:44 AM
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).

11-16-2006, 02:36 PM
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

Amit Doshi
08-17-2009, 12:18 PM
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")

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

08-17-2009, 12:32 PM
Why has this got so complicated? It's just return false; in whatever control you're using...

12-05-2009, 11:25 PM
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:

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:

$('#emailform').submit(function(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:

<form onsubmit="doSomething" action="..." >
function doSomething(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.

Philip M
12-06-2009, 09:55 AM
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

09-15-2010, 11:03 PM
<form name="test" onsubmit="return test()">
<input type="submit" value="Testing"></input>

Js :

return false;

09-15-2010, 11:06 PM
@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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum