[jQuery] [form] Hack to prevent jquery.form executing script twice
When I set up a form that did file uploads through the excellent
jquery.form plugin, I came across 2 issues:
1) I could not "detect" whether or not an iframe was used.
2) When uploading files, the behavior was different from a normal form
submit because all the javascript in the response was executed twice.
Note that my form submit returns plain html and script (which is
perfectly acceptable for the plugin when not uploading files).
I wanted the plugin to be transparent for both uploads and non-upload
submits. Also, I wanted to know when the iframe hack was being used.
This led me to hack jquery.form like this:
1) I added line 308:
$.extend(options.extraData, { _iframe:1 });
(the modification above could be integrated in the options, made
variable, etc -- but this works fine for me and I can reuse this in
case other iframe hacks occur to let my application know the iframe
hack is working)
2) I modified thes lines 347-354 like this:
if (opts.dataType == 'xml' && !xhr.responseXML) {
if (xhr.responseText != null)
xhr.responseXML = toXml(xhr.responseText);
} else {
var ta = doc.getElementsByTagName('textarea')[0];
xhr.responseText = ta ? ta.value : xhr.responseText;
}
data = $.httpData(xhr, opts.dataType);
Basically, this means when opts.datatype=null (default), you also have
the textarea output hack at your disposal. As far as I can see, this
makes the plugin perfectly transparent. However, I haven't done a full
test. I just thought I'd share my experiences with the authors and the
community.
Dylan