-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery-wname.js
86 lines (72 loc) · 1.87 KB
/
jquery-wname.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* window.name-based transport for jQuery.Ajax with crossdomain file's uploading support
* @version 1.0.3
* @requires jquery >= 1.5
*
* Copyright (c) 2011 Filatov Dmitry (dfilatov@yandex-team.ru)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
(function($) {
$.ajaxPrefilter(function(opts) {
if(opts.files && opts.files.length) {
opts.type = 'POST';
return 'wname';
}
});
var cnt = 1;
$.ajaxTransport('wname', function(opts, origOpts) {
var iframe;
return {
send : function(_, completeCallback) {
var id = '__target_' + (cnt++),
form = $('<form/>')
.css('display', 'none')
.attr({
action : opts.url,
method : 'post',
target : id,
enctype : 'multipart/form-data',
encoding : 'multipart/form-data'
});
$.each($.extend({ _wname : 1 }, origOpts.data), function(name, val) {
form.append($('<input/>').attr({ type : 'hidden', name : name, value : val }));
});
opts.files && opts.files.each(function() {
var file = $(this);
form.append(file.replaceWith(file.clone(true, true)));
});
iframe = $('<iframe name="' + id + '"/>')
.css('display', 'none')
.appendTo('body');
form
.appendTo('body')
.submit()
.remove();
iframe.load(function() {
var frameWin = iframe[0].contentWindow ||
(iframe[0].contentDocument && iframe[0].contentDocument.window);
iframe
.unbind()
.load(function() {
if(frameWin.name != id) {
var resp = frameWin.name;
iframe.remove();
completeCallback(200, 'OK', { wname : resp });
}
else {
iframe.remove();
completeCallback(500, 'INTERNAL_ERROR');
}
});
frameWin.location = 'about:blank';
});
},
abort : function() {
iframe.remove();
}
}
});
})(jQuery);