jquery.iframe-transport.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * jQuery Iframe Transport Plugin 1.5
  3. * https://github.com/blueimp/jQuery-File-Upload
  4. *
  5. * Copyright 2011, Sebastian Tschan
  6. * https://blueimp.net
  7. *
  8. * Licensed under the MIT license:
  9. * http://www.opensource.org/licenses/MIT
  10. */
  11. /*jslint unparam: true, nomen: true */
  12. /*global define, window, document */
  13. (function (factory) {
  14. 'use strict';
  15. if (typeof define === 'function' && define.amd) {
  16. // Register as an anonymous AMD module:
  17. define(['jquery'], factory);
  18. } else {
  19. // Browser globals:
  20. factory(window.jQuery);
  21. }
  22. }(function ($) {
  23. 'use strict';
  24. // Helper variable to create unique names for the transport iframes:
  25. var counter = 0;
  26. // The iframe transport accepts three additional options:
  27. // options.fileInput: a jQuery collection of file input fields
  28. // options.paramName: the parameter name for the file form data,
  29. // overrides the name property of the file input field(s),
  30. // can be a string or an array of strings.
  31. // options.formData: an array of objects with name and value properties,
  32. // equivalent to the return data of .serializeArray(), e.g.:
  33. // [{name: 'a', value: 1}, {name: 'b', value: 2}]
  34. $.ajaxTransport('iframe', function (options) {
  35. if (options.async && (options.type === 'POST' || options.type === 'GET')) {
  36. var form,
  37. iframe;
  38. return {
  39. send: function (_, completeCallback) {
  40. form = $('<form style="display:none;"></form>');
  41. form.attr('accept-charset', options.formAcceptCharset);
  42. // javascript:false as initial iframe src
  43. // prevents warning popups on HTTPS in IE6.
  44. // IE versions below IE8 cannot set the name property of
  45. // elements that have already been added to the DOM,
  46. // so we set the name along with the iframe HTML markup:
  47. iframe = $(
  48. '<iframe src="javascript:false;" name="iframe-transport-' +
  49. (counter += 1) + '"></iframe>'
  50. ).bind('load', function () {
  51. var fileInputClones,
  52. paramNames = $.isArray(options.paramName) ?
  53. options.paramName : [options.paramName];
  54. iframe
  55. .unbind('load')
  56. .bind('load', function () {
  57. var response;
  58. // Wrap in a try/catch block to catch exceptions thrown
  59. // when trying to access cross-domain iframe contents:
  60. try {
  61. response = iframe.contents();
  62. // Google Chrome and Firefox do not throw an
  63. // exception when calling iframe.contents() on
  64. // cross-domain requests, so we unify the response:
  65. if (!response.length || !response[0].firstChild) {
  66. throw new Error();
  67. }
  68. } catch (e) {
  69. response = undefined;
  70. }
  71. // The complete callback returns the
  72. // iframe content document as response object:
  73. completeCallback(
  74. 200,
  75. 'success',
  76. {'iframe': response}
  77. );
  78. // Fix for IE endless progress bar activity bug
  79. // (happens on form submits to iframe targets):
  80. $('<iframe src="javascript:false;"></iframe>')
  81. .appendTo(form);
  82. form.remove();
  83. });
  84. form
  85. .prop('target', iframe.prop('name'))
  86. .prop('action', options.url)
  87. .prop('method', options.type);
  88. if (options.formData) {
  89. $.each(options.formData, function (index, field) {
  90. $('<input type="hidden"/>')
  91. .prop('name', field.name)
  92. .val(field.value)
  93. .appendTo(form);
  94. });
  95. }
  96. if (options.fileInput && options.fileInput.length &&
  97. options.type === 'POST') {
  98. fileInputClones = options.fileInput.clone();
  99. // Insert a clone for each file input field:
  100. options.fileInput.after(function (index) {
  101. return fileInputClones[index];
  102. });
  103. if (options.paramName) {
  104. options.fileInput.each(function (index) {
  105. $(this).prop(
  106. 'name',
  107. paramNames[index] || options.paramName
  108. );
  109. });
  110. }
  111. // Appending the file input fields to the hidden form
  112. // removes them from their original location:
  113. form
  114. .append(options.fileInput)
  115. .prop('enctype', 'multipart/form-data')
  116. // enctype must be set as encoding for IE:
  117. .prop('encoding', 'multipart/form-data');
  118. }
  119. form.submit();
  120. // Insert the file input fields at their original location
  121. // by replacing the clones with the originals:
  122. if (fileInputClones && fileInputClones.length) {
  123. options.fileInput.each(function (index, input) {
  124. var clone = $(fileInputClones[index]);
  125. $(input).prop('name', clone.prop('name'));
  126. clone.replaceWith(input);
  127. });
  128. }
  129. });
  130. form.append(iframe).appendTo(document.body);
  131. },
  132. abort: function () {
  133. if (iframe) {
  134. // javascript:false as iframe src aborts the request
  135. // and prevents warning popups on HTTPS in IE6.
  136. // concat is used to avoid the "Script URL" JSLint error:
  137. iframe
  138. .unbind('load')
  139. .prop('src', 'javascript'.concat(':false;'));
  140. }
  141. if (form) {
  142. form.remove();
  143. }
  144. }
  145. };
  146. }
  147. });
  148. // The iframe transport returns the iframe content document as response.
  149. // The following adds converters from iframe to text, json, html, and script:
  150. $.ajaxSetup({
  151. converters: {
  152. 'iframe text': function (iframe) {
  153. return $(iframe[0].body).text();
  154. },
  155. 'iframe json': function (iframe) {
  156. return $.parseJSON($(iframe[0].body).text());
  157. },
  158. 'iframe html': function (iframe) {
  159. return $(iframe[0].body).html();
  160. },
  161. 'iframe script': function (iframe) {
  162. return $.globalEval($(iframe[0].body).text());
  163. }
  164. }
  165. });
  166. }));