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 12/*jslint unparam: true, nomen: true */ 13/*global define, window, document */ 14 15(function (factory) { 16 'use strict'; 17 if (typeof define === 'function' && define.amd) { 18 // Register as an anonymous AMD module: 19 define(['jquery'], factory); 20 } else { 21 // Browser globals: 22 factory(window.jQuery); 23 } 24}(function ($) { 25 'use strict'; 26 27 // Helper variable to create unique names for the transport iframes: 28 var counter = 0; 29 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')) { 40 var form, 41 iframe; 42 return { 43 send: function (_, completeCallback) { 44 form = $('<form style="display:none;"></form>'); 45 form.attr('accept-charset', options.formAcceptCharset); 46 // javascript:false as initial iframe src 47 // prevents warning popups on HTTPS in IE6. 48 // IE versions below IE8 cannot set the name property of 49 // elements that have already been added to the DOM, 50 // so we set the name along with the iframe HTML markup: 51 iframe = $( 52 '<iframe src="javascript:false;" name="iframe-transport-' + 53 (counter += 1) + '"></iframe>' 54 ).bind('load', function () { 55 var fileInputClones, 56 paramNames = $.isArray(options.paramName) ? 57 options.paramName : [options.paramName]; 58 iframe 59 .unbind('load') 60 .bind('load', function () { 61 var response; 62 // Wrap in a try/catch block to catch exceptions thrown 63 // when trying to access cross-domain iframe contents: 64 try { 65 response = iframe.contents(); 66 // Google Chrome and Firefox do not throw an 67 // exception when calling iframe.contents() on 68 // cross-domain requests, so we unify the response: 69 if (!response.length || !response[0].firstChild) { 70 throw new Error(); 71 } 72 } catch (e) { 73 response = undefined; 74 } 75 // The complete callback returns the 76 // iframe content document as response object: 77 completeCallback( 78 200, 79 'success', 80 {'iframe': response} 81 ); 82 // Fix for IE endless progress bar activity bug 83 // (happens on form submits to iframe targets): 84 $('<iframe src="javascript:false;"></iframe>') 85 .appendTo(form); 86 form.remove(); 87 }); 88 form 89 .prop('target', iframe.prop('name')) 90 .prop('action', options.url) 91 .prop('method', options.type); 92 if (options.formData) { 93 $.each(options.formData, function (index, field) { 94 $('<input type="hidden"/>') 95 .prop('name', field.name) 96 .val(field.value) 97 .appendTo(form); 98 }); 99 } 100 if (options.fileInput && options.fileInput.length && 101 options.type === 'POST') { 102 fileInputClones = options.fileInput.clone(); 103 // Insert a clone for each file input field: 104 options.fileInput.after(function (index) { 105 return fileInputClones[index]; 106 }); 107 if (options.paramName) { 108 options.fileInput.each(function (index) { 109 $(this).prop( 110 'name', 111 paramNames[index] || options.paramName 112 ); 113 }); 114 } 115 // Appending the file input fields to the hidden form 116 // removes them from their original location: 117 form 118 .append(options.fileInput) 119 .prop('enctype', 'multipart/form-data') 120 // enctype must be set as encoding for IE: 121 .prop('encoding', 'multipart/form-data'); 122 } 123 form.submit(); 124 // Insert the file input fields at their original location 125 // by replacing the clones with the originals: 126 if (fileInputClones && fileInputClones.length) { 127 options.fileInput.each(function (index, input) { 128 var clone = $(fileInputClones[index]); 129 $(input).prop('name', clone.prop('name')); 130 clone.replaceWith(input); 131 }); 132 } 133 }); 134 form.append(iframe).appendTo(document.body); 135 }, 136 abort: function () { 137 if (iframe) { 138 // javascript:false as iframe src aborts the request 139 // and prevents warning popups on HTTPS in IE6. 140 // concat is used to avoid the "Script URL" JSLint error: 141 iframe 142 .unbind('load') 143 .prop('src', 'javascript'.concat(':false;')); 144 } 145 if (form) { 146 form.remove(); 147 } 148 } 149 }; 150 } 151 });
152 153 // The iframe transport returns the iframe content document as response. 154 // The following adds converters from iframe to text, json, html, and script: 155 $.ajaxSetup({ 156 converters: { 157 'iframe text': function (iframe) { 158 return $(iframe[0].body).text(); 159 }, 160 'iframe json': function (iframe) { 161 return $.parseJSON($(iframe[0].body).text()); 162 }, 163 'iframe html': function (iframe) { 164 return $(iframe[0].body).html(); 165 }, 166 'iframe script': function (iframe) { 167 return $.globalEval($(iframe[0].body).text()); 168 } 169 } 170 }); 171 172}));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.