On Dec 3, 11:39 pm, (e-mail address removed) wrote:
Below is my current version of the simple serializeFormUrl function.
I've changed the documentation format. In the dependency section "a"
means a required and assumed feature, "r" means a required and tested
feature. I don't mind if the documentation system evolves over the
implementation of the first few interfaces. If this is on the right
track that is fine with me for now.
I removed the use of c.push() in favor of c[c.length]. This change
reduces dependencies, is faster (at least in FF2), reduces code size
(no need for feature test), and does not require any extra variables.
I reduce the indent to two spaces since usenet has such short line
lengths (approx 70 chars). I personally prefer four spaces but I think
two will be better for this project.
If there are any remaining offenses in this implementation of the
interface that make this implementation inadequate for recommendation,
please post an edited version.
/**
* @object serializeFormUrl [function]
* All elements of the form that have a non-empty name
* attribute and are not disabled will be serialized.
*
* Serialize form data in a format similar to the
* <a href="
http://www.w3.org/TR/1999/REC-html401-19991224/interact/
forms.html#h-17.13.4.1">
* application/x-www-form-urlencoded</a> standardized format.
*
* The handling of whitespace different from the standard.
* According to the x-www-form-urlencoded standard,
* a space should be encoded as a "+" and a newline
* should be encoded as a "%0D%0A".
*
* The JavaScript encodeURIComponent function is used
* to encode the form data. A space is encoded as "%20".
* On Windows operating system a new line is encoded
* as "%0D%0A". On Mac OS X a new line is encded as
* just "%0A".
*
* @param f [form]
* The form element to be serialized.
*
* @returns [string]
* The serialized form.
*
* @dependencies
* r - ES3 - encodeURIComponent
* a - ES1 - Array.prototype.join
* a - DOM1 - HTMLFormElement.elements
* a - DOM1 - HTMLFormElement.elements.length
* a - DOM1 - HTMLFormElement.elements
.name
* a - DOM1 - HTMLFormElement.elements.value
* a - DOM1 - HTMLFormElement.elements.disabled
*/
// test for required feature
if (typeof encodeURIComponent != 'undefined') {
var serializeFormUrl = function(f) {
var i, // elements loop index
ilen, // elements loop length
e, // element
es = f.elements,
c = []; // the serialized name=value pairs
for (i=0, ilen=es.length; i<ilen; i++) {
e = es;
if (e.name && !e.disabled) {
c[c.length] = encodeURIComponent(e.name) + "=" +
encodeURIComponent(e.value);
}
}
return c.join('&');
};
}
If two inputs have the same name, the result will be incorrect.
How so?