jquery.iframe-transport.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * jQuery Iframe Transport Plugin 1.6.1
  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) {
  36. var form,
  37. iframe,
  38. addParamChar;
  39. return {
  40. send: function (_, completeCallback) {
  41. form = $('<form style="display:none;"></form>');
  42. form.attr('accept-charset', options.formAcceptCharset);
  43. addParamChar = /\?/.test(options.url) ? '&' : '?';
  44. // XDomainRequest only supports GET and POST:
  45. if (options.type === 'DELETE') {
  46. options.url = options.url + addParamChar + '_method=DELETE';
  47. options.type = 'POST';
  48. } else if (options.type === 'PUT') {
  49. options.url = options.url + addParamChar + '_method=PUT';
  50. options.type = 'POST';
  51. } else if (options.type === 'PATCH') {
  52. options.url = options.url + addParamChar + '_method=PATCH';
  53. options.type = 'POST';
  54. }
  55. // javascript:false as initial iframe src
  56. // prevents warning popups on HTTPS in IE6.
  57. // IE versions below IE8 cannot set the name property of
  58. // elements that have already been added to the DOM,
  59. // so we set the name along with the iframe HTML markup:
  60. iframe = $(
  61. '<iframe src="javascript:false;" name="iframe-transport-' +
  62. (counter += 1) + '"></iframe>'
  63. ).bind('load', function () {
  64. var fileInputClones,
  65. paramNames = $.isArray(options.paramName) ?
  66. options.paramName : [options.paramName];
  67. iframe
  68. .unbind('load')
  69. .bind('load', function () {
  70. var response;
  71. // Wrap in a try/catch block to catch exceptions thrown
  72. // when trying to access cross-domain iframe contents:
  73. try {
  74. response = iframe.contents();
  75. // Google Chrome and Firefox do not throw an
  76. // exception when calling iframe.contents() on
  77. // cross-domain requests, so we unify the response:
  78. if (!response.length || !response[0].firstChild) {
  79. throw new Error();
  80. }
  81. } catch (e) {
  82. response = undefined;
  83. }
  84. // The complete callback returns the
  85. // iframe content document as response object:
  86. completeCallback(
  87. 200,
  88. 'success',
  89. {'iframe': response}
  90. );
  91. // Fix for IE endless progress bar activity bug
  92. // (happens on form submits to iframe targets):
  93. $('<iframe src="javascript:false;"></iframe>')
  94. .appendTo(form);
  95. form.remove();
  96. });
  97. form
  98. .prop('target', iframe.prop('name'))
  99. .prop('action', options.url)
  100. .prop('method', options.type);
  101. if (options.formData) {
  102. $.each(options.formData, function (index, field) {
  103. $('<input type="hidden"/>')
  104. .prop('name', field.name)
  105. .val(field.value)
  106. .appendTo(form);
  107. });
  108. }
  109. if (options.fileInput && options.fileInput.length &&
  110. options.type === 'POST') {
  111. fileInputClones = options.fileInput.clone();
  112. // Insert a clone for each file input field:
  113. options.fileInput.after(function (index) {
  114. return fileInputClones[index];
  115. });
  116. if (options.paramName) {
  117. options.fileInput.each(function (index) {
  118. $(this).prop(
  119. 'name',
  120. paramNames[index] || options.paramName
  121. );
  122. });
  123. }
  124. // Appending the file input fields to the hidden form
  125. // removes them from their original location:
  126. form
  127. .append(options.fileInput)
  128. .prop('enctype', 'multipart/form-data')
  129. // enctype must be set as encoding for IE:
  130. .prop('encoding', 'multipart/form-data');
  131. }
  132. form.submit();
  133. // Insert the file input fields at their original location
  134. // by replacing the clones with the originals:
  135. if (fileInputClones && fileInputClones.length) {
  136. options.fileInput.each(function (index, input) {
  137. var clone = $(fileInputClones[index]);
  138. $(input).prop('name', clone.prop('name'));
  139. clone.replaceWith(input);
  140. });
  141. }
  142. });
  143. form.append(iframe).appendTo(document.body);
  144. },
  145. abort: function () {
  146. if (iframe) {
  147. // javascript:false as iframe src aborts the request
  148. // and prevents warning popups on HTTPS in IE6.
  149. // concat is used to avoid the "Script URL" JSLint error:
  150. iframe
  151. .unbind('load')
  152. .prop('src', 'javascript'.concat(':false;'));
  153. }
  154. if (form) {
  155. form.remove();
  156. }
  157. }
  158. };
  159. }
  160. });
  161. // The iframe transport returns the iframe content document as response.
  162. // The following adds converters from iframe to text, json, html, and script:
  163. $.ajaxSetup({
  164. converters: {
  165. 'iframe text': function (iframe) {
  166. return iframe && $(iframe[0].body).text();
  167. },
  168. 'iframe json': function (iframe) {
  169. return iframe && $.parseJSON($(iframe[0].body).text());
  170. },
  171. 'iframe html': function (iframe) {
  172. return iframe && $(iframe[0].body).html();
  173. },
  174. 'iframe script': function (iframe) {
  175. return iframe && $.globalEval($(iframe[0].body).text());
  176. }
  177. }
  178. });
  179. }));