2 * jQuery Iframe Transport Plugin 1.6.1
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) {
44 send: function (_, completeCallback) {
45 form = $('<form style="display:none;"></form>');
46 form.attr('accept-charset', options.formAcceptCharset);
47 addParamChar = /\?/.test(options.url) ? '&' : '?';
48 // XDomainRequest only supports GET and POST:
49 if (options.type === 'DELETE') {
50 options.url = options.url + addParamChar + '_method=DELETE';
51 options.type = 'POST';
52 } else if (options.type === 'PUT') {
53 options.url = options.url + addParamChar + '_method=PUT';
54 options.type = 'POST';
55 } else if (options.type === 'PATCH') {
56 options.url = options.url + addParamChar + '_method=PATCH';
57 options.type = 'POST';
59 // javascript:false as initial iframe src
60 // prevents warning popups on HTTPS in IE6.
61 // IE versions below IE8 cannot set the name property of
62 // elements that have already been added to the DOM,
63 // so we set the name along with the iframe HTML markup:
65 '<iframe src="javascript:false;" name="iframe-transport-' +
66 (counter += 1) + '"></iframe>'
67 ).bind('load', function () {
69 paramNames = $.isArray(options.paramName) ?
70 options.paramName : [options.paramName];
73 .bind('load', function () {
75 // Wrap in a try/catch block to catch exceptions thrown
76 // when trying to access cross-domain iframe contents:
78 response = iframe.contents();
79 // Google Chrome and Firefox do not throw an
80 // exception when calling iframe.contents() on
81 // cross-domain requests, so we unify the response:
82 if (!response.length || !response[0].firstChild) {
88 // The complete callback returns the
89 // iframe content document as response object:
95 // Fix for IE endless progress bar activity bug
96 // (happens on form submits to iframe targets):
97 $('<iframe src="javascript:false;"></iframe>')
102 .prop('target', iframe.prop('name'))
103 .prop('action', options.url)
104 .prop('method', options.type);
105 if (options.formData) {
106 $.each(options.formData, function (index, field) {
107 $('<input type="hidden"/>')
108 .prop('name', field.name)
113 if (options.fileInput && options.fileInput.length &&
114 options.type === 'POST') {
115 fileInputClones = options.fileInput.clone();
116 // Insert a clone for each file input field:
117 options.fileInput.after(function (index) {
118 return fileInputClones[index];
120 if (options.paramName) {
121 options.fileInput.each(function (index) {
124 paramNames[index] || options.paramName
128 // Appending the file input fields to the hidden form
129 // removes them from their original location:
131 .append(options.fileInput)
132 .prop('enctype', 'multipart/form-data')
133 // enctype must be set as encoding for IE:
134 .prop('encoding', 'multipart/form-data');
137 // Insert the file input fields at their original location
138 // by replacing the clones with the originals:
139 if (fileInputClones && fileInputClones.length) {
140 options.fileInput.each(function (index, input) {
141 var clone = $(fileInputClones[index]);
142 $(input).prop('name', clone.prop('name'));
143 clone.replaceWith(input);
147 form.append(iframe).appendTo(document.body);
151 // javascript:false as iframe src aborts the request
152 // and prevents warning popups on HTTPS in IE6.
153 // concat is used to avoid the "Script URL" JSLint error:
156 .prop('src', 'javascript'.concat(':false;'));
166 // The iframe transport returns the iframe content document as response.
167 // The following adds converters from iframe to text, json, html, and script:
170 'iframe text': function (iframe) {
171 return iframe && $(iframe[0].body).text();
173 'iframe json': function (iframe) {
174 return iframe && $.parseJSON($(iframe[0].body).text());
176 'iframe html': function (iframe) {
177 return iframe && $(iframe[0].body).html();
179 'iframe script': function (iframe) {
180 return iframe && $.globalEval($(iframe[0].body).text());