views:

145

answers:

3

If a user creates a form without a method attribute, it seems like most browsers will handle this at the time of form submission. So upon inspection of the form element after the DOM is ready, you can see that there is no "method" attr of the form element object.

IE7, however, apparently sets a default method value of "GET" on all forms without a method value. I don't want to argue about whether GET or POST is the most sensible default, I just want to find a way to make POST the default form method across all browsers.

My problem is that I can't tell if the user entered a "GET" value for a form method, or if IE injected that value as default. If there is no method attribute of the form, it is obvious that the users didn't specify one, so I can safely default it to POST. But if I see a GET value for a form method, I can't tell if the user specified that, or if it was left black and IE7 set GET when it parsed the HTML.

Anyone have any ideas?

+2  A: 

Run a simple regex on the outerHTML property, since IE doesn't alter the HTML.

if (isIE)
    // true if set, false if not
    var hasMethodAttr = /method=/i.test(form.outerHTML); 
Andy E
So long as the text 'method=' doesn't appear *anywhere* inside the form's content or inputs
Gareth
Well there shouldn't be any forms nested within forms, right? And there are no other html elements with a "method" attr, so hopefully this is safe. :/
Matthew Taylor
Come to think of it, this is still not safe, because there is nothing from stopping someone from entering "method=" in an input field. My regex will have to only match within the opening form tag.
Matthew Taylor
+1  A: 

This doesn't seem to be in violation of the HTML form spec, which states:

This attribute specifies which HTTP method will be used to submit the form data set. Possible (case-insensitive) values are "get" (the default) and "post". See the section on form submission for usage information

Gareth
Swapped your quote from code formatting to blockquote, hope you don't mind but it looks a bit weird with various words in different colours :-)
Andy E
+6  A: 

IE's behaviour is correct!(*) According to DTD:

method      (GET|POST)     GET       -- HTTP method used to submit the form--

or, in the XHTML DTD:

method      (get|post)     "get"

that means if the method attribute is omitted, not only does the form submit as GET by default, but the DOM actually should contain an Attr node for method with the DTD defaulted value GET.

(*: well, sort of. IE is using the XHTML lower-case default in an HTML document where it should be the upper-case. Not that it really matters as the attribute is case-insensitive in HTML anyhow. And hey! It's IE getting the standard more-right than all the other browsers. It's a miracle!)

So how do you tell that the Attr node was put there because of DTD attribute defaulting and not because it was in the source? With the DOM Level 1 Core specified flag:

var form= document.getElementById('myform');
var attr= form.getAttributeNode('method');
var isomitted= attr===null || !attr.specified;
bobince
+1, a better answer than mine and I learned something. `attr` is never `null` in the final line (for the `method` attribute at least) in IE so you could probably just live with `!attr.specified`
Andy E
Yeah, the idea is to accommodate the other browsers with that test.
bobince