Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #17062

Re: the right "form" element for ajax

Newsgroups comp.lang.javascript
Date 2012-11-06 20:23 -0800
References <015e6c83-d721-441b-8e07-5393238fe005@googlegroups.com>
Message-ID <299f4f19-8e31-40b7-bb11-85f583f17e57@googlegroups.com> (permalink)
Subject Re: the right "form" element for ajax
From RobG <rgqld@iinet.net.au>

Show all headers | View raw


On Wednesday, November 7, 2012 2:19:38 AM UTC+10, amado...@gmail.com wrote:
[...]
> When we send user data via AJAX the attribute "action" of the HTML 
> element "form" is useless and even looks like it can get in the way.
> Usually we call the send data function right from the "onclick" event
> of a "button" element, and use the getElement functions to fetch the
> user input. There's no "action" in the "form". Actually we don't even
> need the "form" element at all. But per the HTML reference

Which reference? There is the W3C HTML 4.01 standard and the various HTML5 specifications (various W3C drafts and WHATWG "HTML Living Standard").


> the "form" element is required

No, it is not. Various elements can be used as form controls in a form, but they are not constrained to always be in a form (in which case they are just plain elements, not form controls). 

e.g. in HTML5, an input element is "phrasing content" and can appear anywhere such content is allowed, just like A, SPAN, STRONG and similar elements.

Forms are convenient for other reasons. For example, in a function called as a listener on a form control, *this* will reference the element and *this.form* will reference the form it's in. Other form controls can then be conveniently referenced as *form.controlName*, rather than *document.getElementById* or similar.

Forms also create a logical grouping of elements, similar to a div.

> and its "action" attribute too (or isn't it?)

It is required in HTML 4.01[1], but not in HTML5[2].

However, a robust strategy is to have the form work without scripting, then add scripting to improve the user experience. 

You should run your script from the form's submit listener. If it completes sucessfully, it can cancel submit and do the AJAX thing. If it does not, it can allow the form to submit. 

If the form doesn't submit, there is no reason that the action attribute should interfere with anything.


> Some browsers accept "input" fields and "button" elements without
> being enclosed in any "form" element.

Per HTML 4.01 and HTML5

> And accept "form" elements without the "action" attribute. But,
> strictly, this is illegal, right?

Not "illegal" but "invalid". It's a good idea to have valid markup, however in some cases the consequences of invalid markup are well known and insignificant. Not having an action attribute is only an issue in a case where one is required (e.g. where the form should be submitted). 


> So, the issue is: what we do? Rely on the permissive behavior of
> browsers, or write legal HTML and [hope] it does not interfere with
> our ajax?

There is no reason for the action attribute to not have a valid value (see above), or for it to interfere with an XMLHttpRequest (or whatever mechnaism you are using for AJAX) since you can cancel the form's submit if required.

> I see the Gmail interface has "form" elements with an empty "action"
> attribute. Would that be the "best practice"?

In HTML5, the action attribute isn't mandatory. In HTML 4 and 5 (if there is an action attribute), an empty value is invalid.

I expect you should just omit the attribute and use <!DOCTYPE html> so that invalidity isn't an issue (in this case).


1. http://www.w3.org/TR/REC-html40/interact/forms.html#edef-FORM
2. http://dev.w3.org/html5/spec/single-page.html#attr-fs-action


-- 
Rob

Back to comp.lang.javascript | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

the right "form" element for ajax amado.alves@gmail.com - 2012-11-06 08:19 -0800
  Re: the right "form" element for ajax Tim Streater <timstreater@greenbee.net> - 2012-11-06 17:08 +0000
  Re: the right "form" element for ajax Martin Leese <please@see.Web.for.e-mail.INVALID> - 2012-11-06 11:02 -0700
    Re: the right "form" element for ajax amado.alves@gmail.com - 2012-11-06 14:35 -0800
    Re: the right "form" element for ajax Tim Streater <timstreater@greenbee.net> - 2012-11-06 22:52 +0000
  Re: the right "form" element for ajax RobG <rgqld@iinet.net.au> - 2012-11-06 20:23 -0800

csiph-web