tags:

views:

54

answers:

5

Hi there,

I have a function called block which simply return false;

this is meant to stop the form being submitted so the ajax functions can capture its submit, however firefox seems to treat it as an actual url and loads the url javascript:block();

if i have action="javascript: return false;" then firefox doesnt even let the ajax functions handle it.

how can i fix this?

+3  A: 

Javascript code doesn't belong in the action attribute. You can use the onsubmit event.

<form onsubmit="return false;" .... >

note however that it's considered good style to specify an action that will work if the user has JavaScript turned off (graceful degradation). If you don't specify an action property, the form will be submitted to the current page URL.

@Justin correctly notes that having JavaScript code in the HTML markup is strictly speaking always a bit dirty. The good practice would be giving the form an id attribute, and assigning the JavaScript code to that element in the head part.

With pure Javascript:

document.onload = function() {
  document.getElementById("formid").onload = function() {  ..... }
}

With jQuery

$(document).ready(function() {
  $("#formid").load(function() { .... });
});
Pekka
JavaScript code doesn't belong in *any* attribute
Justin Johnson
@Justin good point. I added some more information.
Pekka
I call b.s. For what other reason are those attributes there for then? Don't be elitist.
Crayon Violent
@Crayon those attributes are old. Mixing JavaScript and HTML *is* not the optimum way any more. Inline Javascript is fine for small pieces of code and quick-and-dirty form setups, but tends to create chaos in anything bigger. I don't think showing how to use inline Javascript is downvote-worthy, either, but the alternative is a good thing to point out.
Pekka
+1  A: 

put the function call in onsubmit="..."

Crayon Violent
A: 
<form method="POST" action="Some url" onSubmit="doAjaxSubmit()">

</form>

The function looks like this

   function doAjaxSubmit() {
      // doe your custom  ajax here.
      // ...
      return false; // so that form does not submit
   }
naikus
Apologies for the typo. This is what happens when you do it in a hurry :)
naikus
The -1 has nothing to do with typos. See my answer and Pekka's correction
Justin Johnson
Their answers are still valid. Just a different way of doing stuff, no reason to downvote them for it.
Brad F Jacobs
I'm sorry to say that voting is a matter of opinion and it is my opinion that you are giving the OP bad advice. Just because an answer works doesn't mean it's a good answer.
Justin Johnson
A: 

You need to take advantage of the onsubmit event of the form element:

<form action="url_to_post_to" onsubmit="block();">
    <!-- your form here -->
</form>
Tim S. Van Haren
A: 

The proper way to attach events is not through HTML attributes, but unobtrusively through JavaScript itself using the traditional or advanced model.

An example of the traditional model

var block = function() {
    ...
};

document.getElementById("my-form").onsubmit = block;
Justin Johnson
that's not the "proper" way that's just "another" way. Putting stuff in html attributes is perfectly valid, that's what they are there for. Stop down-voting other people just because you have your own style.
Crayon Violent
It's technically valid as part of the HTML specification, but it's a bad practice to combine structure (HTML) and logic (JavaScrpt). Everyone knows this, I don't know why it's such a big surprise or a tender subject. I'll down vote you all I want if I disagree with you. That's how voting works.
Justin Johnson