1/* 2 * jQuery.upload v1.0.3 3 * 4 * Copyright (c) 2013 Bass Jobsen 5 * http://www.w3masters.nl/ 6 * Dual licensed under the MIT and GPL licenses. 7 * 8 * Original written by Lagos (http://lagoscript.org/) 9 * 10 * Contributors: 11 * Mr Rogers http://rcode5.wordpress.com/ 12 */ 13 14(function($) { 15 16 var uuid = 0; 17 18 $.fn.upload = function(url, data, callback, type) { 19 var self = this, inputs, checkbox, checked, 20 iframeName = 'jquery_upload' + ++uuid, 21 iframe = $('<iframe name="' + iframeName + '" style="position:absolute;top:-9999px" />').appendTo('body'), 22 form = '<form target="' + iframeName + '" method="post" enctype="multipart/form-data" />'; 23 24 if ($.isFunction(data)) { 25 type = callback; 26 callback = data; 27 data = {}; 28 } 29 30 checkbox = $('input:checkbox', this); 31 checked = $('input:checked', this); 32 form = self.wrapAll(form).parent('form').attr('action', url); 33 34 // Make sure radios and checkboxes keep original values 35 // (IE resets checkd attributes when appending) 36 checkbox.removeAttr('checked'); 37 checked.attr('checked', true); 38 39 inputs = createInputs(data); 40 inputs = inputs ? $(inputs).appendTo(form) : null; 41 42 form.submit(function() { 43 iframe.load(function() { 44 var data = handleData(this, type), 45 checked = $('input:checked', self); 46 47 form.after(self).remove(); 48 checkbox.removeAttr('checked'); 49 checked.attr('checked', true); 50 if (inputs) { 51 inputs.remove(); 52 } 53 54 setTimeout(function() { 55 iframe.remove(); 56 if (type === 'script') { 57 $.globalEval(data); 58 } 59 if (callback) { 60 callback.call(self, data); 61 } 62 }, 0); 63 }); 64 }).submit(); 65 66 return this; 67 }; 68 69 function createInputs(data) { 70 return $.map(param(data), function(param) { 71 var e = $(document.createElement('input')); 72 e.attr('type', 'hidden'); 73 e.attr('name', param.name); 74 e.attr('value', param.value); 75 return e; 76 }).join(''); 77 } 78 79 function param(data) { 80 if ($.isArray(data)) { 81 return data; 82 } 83 var params = []; 84 85 function add(name, value) { 86 params.push({name:name, value:value}); 87 } 88 89 if (typeof data === 'object') { 90 $.each(data, function(name) { 91 if ($.isArray(this)) { 92 $.each(this, function() { 93 add(name, this); 94 }); 95 } else { 96 add(name, $.isFunction(this) ? this() : this); 97 } 98 }); 99 } else if (typeof data === 'string') { 100 $.each(data.split('&'), function() { 101 var param = $.map(this.split('='), function(v) { 102 return decodeURIComponent(v.replace(/\+/g, ' ')); 103 }); 104 105 add(param[0], param[1]); 106 }); 107 } 108 109 return params; 110 } 111 112 function handleData(iframe, type) { 113 var data, contents = $(iframe).contents().get(0); 114 115 if ($.isXMLDoc(contents) || contents.XMLDocument) { 116 return contents.XMLDocument || contents; 117 } 118 data = $(contents).find('body').html(); 119 switch (type) { 120 case 'xml': 121 data = parseXml(data); 122 break; 123 case 'json': 124 data = $.parseJSON(data); 125 break; 126 } 127 return data; 128 } 129 130 function parseXml(text) { 131 if (window.DOMParser) { 132 return new DOMParser().parseFromString(text, 'application/xml'); 133 } else { 134 var xml = new ActiveXObject('Microsoft.XMLDOM'); 135 xml.async = false; 136 xml.loadXML(text); 137 return xml; 138 } 139 } 140 141})(jQuery);
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.