2 * jQuery Iframe Transport Plugin 1.5
3 * https://github.com/blueimp/jQuery-File-Upload
5 * Copyright 2011, Sebastian Tschan
8 * Licensed under the MIT license:
9 * http://www.opensource.org/licenses/MIT
12 /*jslint unparam: true, nomen: true */
13 /*global define, window, document */
17 if (typeof define === 'function' && define.amd) {
18 // Register as an anonymous AMD module:
19 define(['jquery'], factory);
22 factory(window.jQuery);
27 // Helper variable to create unique names for the transport iframes:
30 // The iframe transport accepts three additional options:
31 // options.fileInput: a jQuery collection of file input fields
32 // options.paramName: the parameter name for the file form data,
33 // overrides the name property of the file input field(s),
34 // can be a string or an array of strings.
35 // options.formData: an array of objects with name and value properties,
36 // equivalent to the return data of .serializeArray(), e.g.:
37 // [{name: 'a', value: 1}, {name: 'b', value: 2}]
38 $.ajaxTransport('iframe', function (options) {
39 if (options.async && (options.type === 'POST' || options.type === 'GET')) {
43 send: function (_, completeCallback) {
44 form = $('<form style="display:none;"></form>');
45 form.attr('accept-charset', options.formAcceptCharset);
46 // javascript:false as initial iframe src
47 // prevents warning popups on HTTPS in IE6.
48 // IE versions below IE8 cannot set the name property of
49 // elements that have already been added to the DOM,
50 // so we set the name along with the iframe HTML markup:
52 '<iframe src="javascript:false;" name="iframe-transport-' +
53 (counter += 1) + '"></iframe>'
54 ).bind('load', function () {
56 paramNames = $.isArray(options.paramName) ?
57 options.paramName : [options.paramName];
60 .bind('load', function () {
62 // Wrap in a try/catch block to catch exceptions thrown
63 // when trying to access cross-domain iframe contents:
65 response = iframe.contents();
66 // Google Chrome and Firefox do not throw an
67 // exception when calling iframe.contents() on
68 // cross-domain requests, so we unify the response:
69 if (!response.length || !response[0].firstChild) {
75 // The complete callback returns the
76 // iframe content document as response object:
82 // Fix for IE endless progress bar activity bug
83 // (happens on form submits to iframe targets):
84 $('<iframe src="javascript:false;"></iframe>')
89 .prop('target', iframe.prop('name'))
90 .prop('action', options.url)
91 .prop('method', options.type);
92 if (options.formData) {
93 $.each(options.formData, function (index, field) {
94 $('<input type="hidden"/>')
95 .prop('name', field.name)
100 if (options.fileInput && options.fileInput.length &&
101 options.type === 'POST') {
102 fileInputClones = options.fileInput.clone();
103 // Insert a clone for each file input field:
104 options.fileInput.after(function (index) {
105 return fileInputClones[index];
107 if (options.paramName) {
108 options.fileInput.each(function (index) {
111 paramNames[index] || options.paramName
115 // Appending the file input fields to the hidden form
116 // removes them from their original location:
118 .append(options.fileInput)
119 .prop('enctype', 'multipart/form-data')
120 // enctype must be set as encoding for IE:
121 .prop('encoding', 'multipart/form-data');
124 // Insert the file input fields at their original location
125 // by replacing the clones with the originals:
126 if (fileInputClones && fileInputClones.length) {
127 options.fileInput.each(function (index, input) {
128 var clone = $(fileInputClones[index]);
129 $(input).prop('name', clone.prop('name'));
130 clone.replaceWith(input);
134 form.append(iframe).appendTo(document.body);
138 // javascript:false as iframe src aborts the request
139 // and prevents warning popups on HTTPS in IE6.
140 // concat is used to avoid the "Script URL" JSLint error:
143 .prop('src', 'javascript'.concat(':false;'));
153 // The iframe transport returns the iframe content document as response.
154 // The following adds converters from iframe to text, json, html, and script:
157 'iframe text': function (iframe) {
158 return $(iframe[0].body).text();
160 'iframe json': function (iframe) {
161 return $.parseJSON($(iframe[0].body).text());
163 'iframe html': function (iframe) {
164 return $(iframe[0].body).html();
166 'iframe script': function (iframe) {
167 return $.globalEval($(iframe[0].body).text());