2 * jQuery Iframe Transport Plugin 1.1
3 * https://github.com/blueimp/jQuery-File-Upload
5 * Copyright 2011, Sebastian Tschan
8 * Licensed under the MIT license:
9 * http://creativecommons.org/licenses/MIT/
12 /*jslint unparam: true */
18 // Helper variable to create unique names for the transport iframes:
21 // The iframe transport accepts two additional options:
22 // options.fileInput: a jQuery collection of file input fields
23 // options.formData: an array of objects with name and value properties,
24 // equivalent to the return data of .serializeArray(), e.g.:
25 // [{name: a, value: 1}, {name: b, value: 2}]
26 $.ajaxTransport('iframe', function (options, originalOptions, jqXHR) {
27 if (options.type === 'POST' || options.type === 'GET') {
31 send: function (headers, completeCallback) {
32 form = $('<form style="display:none;"></form>');
33 // javascript:false as initial iframe src
34 // prevents warning popups on HTTPS in IE6.
35 // IE versions below IE8 cannot set the name property of
36 // elements that have already been added to the DOM,
37 // so we set the name along with the iframe HTML markup:
39 '<iframe src="javascript:false;" name="iframe-transport-' +
40 (counter += 1) + '"></iframe>'
41 ).bind('load', function () {
45 .bind('load', function () {
46 // The complete callback returns the
47 // iframe content document as response object:
51 {'iframe': iframe.contents()}
53 // Fix for IE endless progress bar activity bug
54 // (happens on form submits to iframe targets):
55 $('<iframe src="javascript:false;"></iframe>')
60 .prop('target', iframe.prop('name'))
61 .prop('action', options.url)
62 .prop('method', options.type);
63 if (options.formData) {
64 $.each(options.formData, function (index, field) {
65 $('<input type="hidden"/>')
66 .prop('name', field.name)
71 if (options.fileInput && options.fileInput.length &&
72 options.type === 'POST') {
73 fileInputClones = options.fileInput.clone();
74 // Insert a clone for each file input field:
75 options.fileInput.after(function (index) {
76 return fileInputClones[index];
78 // Appending the file input fields to the hidden form
79 // removes them from their original location:
81 .append(options.fileInput)
82 .prop('enctype', 'multipart/form-data')
83 // enctype must be set as encoding for IE:
84 .prop('encoding', 'multipart/form-data');
87 // Insert the file input fields at their original location
88 // by replacing the clones with the originals:
89 if (fileInputClones && fileInputClones.length) {
90 options.fileInput.each(function (index, input) {
91 $(fileInputClones[index]).replaceWith(input);
95 form.append(iframe).appendTo('body');
99 // javascript:false as iframe src aborts the request
100 // and prevents warning popups on HTTPS in IE6.
101 // concat is used to avoid the "Script URL" JSLint error:
104 .prop('src', 'javascript'.concat(':false;'));
114 // The iframe transport returns the iframe content document as response.
115 // The following adds converters from iframe to text, json, html, and script:
118 'iframe text': function (iframe) {
119 return iframe.text();
121 'iframe json': function (iframe) {
122 return $.parseJSON(iframe.text());
124 'iframe html': function (iframe) {
125 return iframe.find('body').html();
127 'iframe script': function (iframe) {
128 return $.globalEval(iframe.text());