...

View Full Version : Javascript stop form submit after pressed



n00ge
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?

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

n00ge
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.

n00ge
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?

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

felgall
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).

fedemika
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")
{
window.document.execCommand('Stop');
}
else
{
window.stop();
}

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

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

cgagliardi
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) {
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:


<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.

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

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

Js :

test()
{
return false;
}

ajith_rock
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.

Best,

--
Ajith



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum