Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 4 votes, 3.75 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-02-2006, 02:36 PM   PM User | #1
n00ge
New Coder

 
Join Date: Jun 2005
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
n00ge is an unknown quantity at this point
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?
n00ge is offline   Reply With Quote
Old 05-02-2006, 03:03 PM   PM User | #2
andyy15
New to the CF scene

 
Join Date: May 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
andyy15 is an unknown quantity at this point
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.
andyy15 is offline   Reply With Quote
Old 05-02-2006, 03:13 PM   PM User | #3
n00ge
New Coder

 
Join Date: Jun 2005
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
n00ge is an unknown quantity at this point
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 is offline   Reply With Quote
Old 05-02-2006, 03:29 PM   PM User | #4
n00ge
New Coder

 
Join Date: Jun 2005
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
n00ge is an unknown quantity at this point
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?
n00ge is offline   Reply With Quote
Old 05-02-2006, 05:35 PM   PM User | #5
arnyinc
Regular Coder

 
Join Date: Jan 2003
Posts: 867
Thanks: 4
Thanked 8 Times in 8 Posts
arnyinc is an unknown quantity at this point
You would "return false" with your onsubmit call.
arnyinc is offline   Reply With Quote
Old 05-02-2006, 11:44 PM   PM User | #6
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,454
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 11-16-2006, 01:36 PM   PM User | #7
fedemika
New to the CF scene

 
Join Date: Nov 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
fedemika is an unknown quantity at this point
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
fedemika is offline   Reply With Quote
Old 08-17-2009, 11:18 AM   PM User | #8
Amit Doshi
New to the CF scene

 
Join Date: Aug 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Amit Doshi is an unknown quantity at this point
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).
Amit Doshi is offline   Reply With Quote
Old 08-17-2009, 11:32 AM   PM User | #9
eddjc
Regular Coder

 
Join Date: Aug 2008
Posts: 104
Thanks: 4
Thanked 14 Times in 14 Posts
eddjc is an unknown quantity at this point
Why has this got so complicated? It's just return false; in whatever control you're using...
eddjc is offline   Reply With Quote
Old 12-05-2009, 10:25 PM   PM User | #10
cgagliardi
New to the CF scene

 
Join Date: Dec 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
cgagliardi is an unknown quantity at this point
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.
cgagliardi is offline   Reply With Quote
Old 12-06-2009, 08:55 AM   PM User | #11
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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
Philip M is online now   Reply With Quote
Old 09-15-2010, 10:03 PM   PM User | #12
ajith_rock
New Coder

 
Join Date: Apr 2007
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
ajith_rock is an unknown quantity at this point
<form name="test" onsubmit="return test()">
.
.
.
.
<input type="submit" value="Testing"></input>
</form>

Js :

test()
{
return false;
}
ajith_rock is offline   Reply With Quote
Old 09-15-2010, 10:06 PM   PM User | #13
ajith_rock
New Coder

 
Join Date: Apr 2007
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
ajith_rock is an unknown quantity at this point
@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
ajith_rock is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:57 AM.


Advertisement
Log in to turn off these ads.