2 * jQuery Iframe Transport Plugin 1.4
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 // javascript:false as initial iframe src
46 // prevents warning popups on HTTPS in IE6.
47 // IE versions below IE8 cannot set the name property of
48 // elements that have already been added to the DOM,
49 // so we set the name along with the iframe HTML markup:
51 '<iframe src="javascript:false;" name="iframe-transport-' +
52 (counter += 1) + '"></iframe>'
53 ).bind('load', function () {
55 paramNames = $.isArray(options.paramName) ?
56 options.paramName : [options.paramName];
59 .bind('load', function () {
61 // Wrap in a try/catch block to catch exceptions thrown
62 // when trying to access cross-domain iframe contents:
64 response = iframe.contents();
65 // Google Chrome and Firefox do not throw an
66 // exception when calling iframe.contents() on
67 // cross-domain requests, so we unify the response:
68 if (!response.length || !response[0].firstChild) {
74 // The complete callback returns the
75 // iframe content document as response object:
81 // Fix for IE endless progress bar activity bug
82 // (happens on form submits to iframe targets):
83 $('<iframe src="javascript:false;"></iframe>')
88 .prop('target', iframe.prop('name'))
89 .prop('action', options.url)
90 .prop('method', options.type);
91 if (options.formData) {
92 $.each(options.formData, function (index, field) {
93 $('<input type="hidden"/>')
94 .prop('name', field.name)
99 if (options.fileInput && options.fileInput.length &&
100 options.type === 'POST') {
101 fileInputClones = options.fileInput.clone();
102 // Insert a clone for each file input field:
103 options.fileInput.after(function (index) {
104 return fileInputClones[index];
106 if (options.paramName) {
107 options.fileInput.each(function (index) {
110 paramNames[index] || options.paramName
114 // Appending the file input fields to the hidden form
115 // removes them from their original location:
117 .append(options.fileInput)
118 .prop('enctype', 'multipart/form-data')
119 // enctype must be set as encoding for IE:
120 .prop('encoding', 'multipart/form-data');
123 // Insert the file input fields at their original location
124 // by replacing the clones with the originals:
125 if (fileInputClones && fileInputClones.length) {
126 options.fileInput.each(function (index, input) {
127 var clone = $(fileInputClones[index]);
128 $(input).prop('name', clone.prop('name'));
129 clone.replaceWith(input);
133 form.append(iframe).appendTo(document.body);
137 // javascript:false as iframe src aborts the request
138 // and prevents warning popups on HTTPS in IE6.
139 // concat is used to avoid the "Script URL" JSLint error:
142 .prop('src', 'javascript'.concat(':false;'));
152 // The iframe transport returns the iframe content document as response.
153 // The following adds converters from iframe to text, json, html, and script:
156 'iframe text': function (iframe) {
157 return $(iframe[0].body).text();
159 'iframe json': function (iframe) {
160 return $.parseJSON($(iframe[0].body).text());
162 'iframe html': function (iframe) {
163 return $(iframe[0].body).html();
165 'iframe script': function (iframe) {
166 return $.globalEval($(iframe[0].body).text());