onsubmit="return false" has no effect on Internet Explorer 7/8 (form is still submitted)

76,737

Solution 1

I'm going to nitpick this. If you want to handle form submissions, that is what submit is for. If the user hits enter in one of your fields, your onclick handler will be totally avoided. Here is a basic example of doing this in a non-obtrusive way.

<form name="myform">
  <input type="submit" />
</form>
<script>
  document.myform.onsubmit = function(){
    alert('handled');
    return false;
  }
</script>

This can be made a lot simpler with jQuery, same form...

$("form[name=myform]").bind('submit',function(){
   alert('handled');
   return false;
});

Solution 2

Several ideas proposed here work (because they use different ways to write correct code), but there is a much easier answer

OP wrote :

onsubmit="submitmyform();"

instead of :

onsubmit="return submitmyform();"

That's it.

Solution 3

I don't think your return false is ever reached, as it comes after what's returned from your function.

<form action="/dosomething.htm" method="GET" onsubmit="submitmyform();return false">

Make sure that you return false inside of your 'submitmyform()' function else, if it's not then it could be returning true to you form obsubmit event.

Solution 4

<script type="text/javascript">
<!--

function submitHandler()
{
  alert(0);
  return false;
}

window.onload=function(){document.getElementById("formid").attachEvent("onsubmit", submitHandler);}
-->
</script>

<form action="/dosomething.htm" method="GET" id="formid">
  [...]
  <input type="submit" value="Go">
</form>

The attach event method only works only for IE7/8. If you want a reliable cross browser solution you should use addEventListener as an alternative.

Hope it helps

Solution 5

try this. work for me.

onSubmit="javascript:return false"

Share:
76,737
Dirk Paessler
Author by

Dirk Paessler

Founder and CEO of Paessler AG, www.paessler.com, a vendor of network monitoring software

Updated on July 09, 2022

Comments

  • Dirk Paessler
    Dirk Paessler almost 2 years

    I have a form that will be submitted by javascript code triggered in "onsubmit" of the tag. Works fine on all browsers - but not on IE7/IE8.

    What can I do?

    <form action="/dosomething.htm" method="GET" onsubmit="submitmyform();return false">
      [...]
      <input type="submit" value="Go">
    </form>
    
  • Andy E
    Andy E over 13 years
    This will only prevent form submissions that occur by clicking or pressing the enter key on that submit button. Pressing the enter key in an input control inside the form would work around it.
  • Andy E
    Andy E over 13 years
    This won't work. The verify() function returns control to the onsubmit function, so it is the onsubmit function that needs to return false to cancel the default action.
  • Dirk Paessler
    Dirk Paessler over 13 years
    I also thought that would be the case. But... that's not what I see here.
  • Lee Kowalkowski
    Lee Kowalkowski almost 12 years
    That's not non-obtrusive either, if you had a mechanism where you can automatically bind an event to a function without knowing any specific implementation details, then you are. I.e. having document.myform or form[name=myform] is just as obtrusive as having an onsubmit attribute in the HTML, except in the opposite direction. What you need is a way of discovering the forms, discovering the validation functions for it (perhaps by naming convention validateMyForm()), and therefore binding automatically and generically. If you really want to be non-obtrusive. It would be reusable too.
  • yoel halb
    yoel halb over 11 years
    @AndyE It does in fact prevent even when hitting enter... check it out
  • Andy E
    Andy E over 11 years
    @yohal: you really should test in more browsers before posting such comments ;-) I just tested the latest Firefox and IE8 on my linux box and neither fire the submit button's onclick event for me. Chrome does, though, but it goes to show that you really can't trust browsers to agree on things unless they're written in the specifications.
  • yoel halb
    yoel halb over 11 years
    @AndyE Sorry I have not tested on linux, but on windows I have tested in all major browsers (IE9, FF, Chrome, Safari and Opera), regarding specifications there are no specifications for onsubmit either, see stackoverflow.com/questions/128923/…
  • yoel halb
    yoel halb about 11 years
    @AndyE See a more complete solution by intercepting the enter key in my answer on stackoverflow.com/questions/10572350/…
  • jinglesthula
    jinglesthula about 10 years
    If you had 2 forms on a page and each required different on submit logic, you'd have to have some way of denoting which form gets which function. I think that's essentially what's been done in this answer via the name attribute. You could also use a data-* attribute. I think for cases like this, 'unobtrusive JavaScript' refers to not using js inline in markup rather than having strictly decoupled markup and JavaScript.