-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.jsonp-1.1.0.js
269 lines (227 loc) · 7.29 KB
/
jquery.jsonp-1.1.0.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* jQuery JSONP Core Plugin 1.1.0 (2009-10-06)
*
* http://code.google.com/p/jquery-jsonp/
*
* Copyright (c) 2009 Julian Aubourg
*
* This document is licensed as free software under the terms of the
* MIT License: http://www.opensource.org/licenses/mit-license.php
*/
(function($){
// ###################### UTILITIES ##
// Test a value is neither undefined nor null
var defined = function(v) {
return v!==undefined && v!==null;
},
// Call if defined
callIfDefined = function(method,object,parameters) {
defined(method) && method.apply(object,parameters);
},
// Let the current thread running
later = function(functor) {
setTimeout(functor,0);
},
// String constants (for better minification)
empty="",
amp="&",
qMark="?",
success = "success",
error = "error",
// Head element (for faster use)
head = $("head"),
// Page cache
pageCache = {},
// ###################### DEFAULT OPTIONS ##
xOptionsDefaults = {
//beforeSend: undefined,
//cache: false,
callback: "C",
//callbackParameter: undefined,
//complete: undefined,
//data: ""
//dataFilter: undefined,
//error: undefined,
//pageCache: false,
//success: undefined,
//timeout: 0,
url: location.href
},
// ###################### MAIN FUNCTION ##
jsonp = function(xOptions) {
// Build data with default
xOptions = $.extend({},xOptionsDefaults,xOptions);
// References to beforeSend (for better minification)
var beforeSendCallback = xOptions.beforeSend,
// Abort/done flag
done = 0;
// Put a temporary abort
xOptions.abort = function() { done = 1; };
// Call beforeSend if provided (early abort if false returned)
if (defined(beforeSendCallback) && (beforeSendCallback(xOptions,xOptions)===false || done))
return xOptions;
// References to xOptions members (for better minification)
var successCallback = xOptions.success,
completeCallback = xOptions.complete,
errorCallback = xOptions.error,
dataFilter = xOptions.dataFilter,
callbackParameter = xOptions.callbackParameter,
successCallbackName = xOptions.callback,
cacheFlag = xOptions.cache,
pageCacheFlag = xOptions.pageCache,
url = xOptions.url,
data = xOptions.data,
timeout = xOptions.timeout,
// Misc variables
splitUrl,splitData,i,j;
// Control entries
url = defined(url)?url:empty;
data = defined(data)?((typeof data)=="string"?data:$.param(data)):empty;
// Add callback parameter if provided as option
defined(callbackParameter)
&& (data += (data==empty?empty:amp)+escape(callbackParameter)+"=?");
// Add anticache parameter if needed
!cacheFlag && !pageCacheFlag
&& (data += (data==empty?empty:amp)+"_"+(new Date()).getTime()+"=");
// Search for ? in url
splitUrl = url.split(qMark);
// Also in parameters if provided
// (and merge arrays)
if (data!=empty) {
splitData = data.split(qMark);
j = splitUrl.length-1;
j && (splitUrl[j] += amp + splitData.shift());
splitUrl = splitUrl.concat(splitData);
}
// If more than 2 ? replace the last one by the callback
i = splitUrl.length-2;
i && (splitUrl[i] += successCallbackName + splitUrl.pop());
// Build the final url
var finalUrl = splitUrl.join(qMark),
// Utility function
notifySuccess = function(json) {
// Apply the data filter if provided
defined(dataFilter) && (json = dataFilter.apply(xOptions,[json]));
// Call success then complete
callIfDefined(successCallback,xOptions,[json,success]);
callIfDefined(completeCallback,xOptions,[xOptions,success]);
},
notifyError = function(type) {
// Call error then complete
callIfDefined(errorCallback,xOptions,[xOptions,type]);
callIfDefined(completeCallback,xOptions,[xOptions,type]);
},
// Get from pageCache
pageCached = pageCache[finalUrl];
// Check page cache
if (pageCacheFlag && defined(pageCached)) {
later(function() {
// If an error was cached
defined(pageCached.s)
? notifySuccess(pageCached.s)
: notifyError(error);
});
return xOptions;
}
// Create & write to the iframe (sends the request)
// We let the hand to current code to avoid
// pre-emptive callbacks
// We also install the timeout here to avoid
// timeout before the code has been dumped to the frame
// (in case of insanely short timeout values)
later(function() {
// If it has been aborted, do nothing
if (done) return;
// Create an iframe & add it to the document
var frame = $("<iframe />").appendTo(head),
// Get the iframe's window and document objects
tmp = frame[0],
window = tmp.contentWindow || tmp.contentDocument,
document = window.document,
// Declaration of cleanup function
cleanUp,
// Declaration of timer for timeout (so we can clear it eventually)
timeoutTimer,
// Error function
errorFunction = function (_,type) {
// If pure error (not timeout), cache if needed
pageCacheFlag && !defined(type) && (pageCache[finalUrl] = empty);
// Cleanup
cleanUp();
// Call error then complete
notifyError(defined(type)?type:error);
},
// Iframe variable cleaning function
removeVariable = function(varName) {
window[varName] = undefined;
try { delete window[varName]; } catch(_) {}
},
// Error callback name
errorCallbackName = successCallbackName=="E"?"X":"E";
// Control if we actually retrieved the document
if(!defined(document)) {
document = window;
window = document.getParentNode();
}
// We have to open the document before
// declaring variables in the iframe's window
// Don't ask me why, I have no clue
document.open();
// Install callbacks
window[successCallbackName] = function(json) {
// Set as treated
done = 1;
// Pagecache is needed
pageCacheFlag && (pageCache[finalUrl] = {s: json});
// Give hand back to frame
// To finish gracefully
later(function(){
// Cleanup
cleanUp();
// Call success then complete
notifySuccess(json);
});
};
window[errorCallbackName] = function(state) {
// If not treated, mark
// then give hand back to iframe
// for it to finish gracefully
(!state || state=="complete") && !done++ && later(errorFunction);
};
// Clean up function (declaration)
xOptions.abort = cleanUp = function() {
// Clear the timeout (is it exists)
clearTimeout(timeoutTimer);
// Open the iframes document & clean
document.open();
removeVariable(errorCallbackName);
removeVariable(successCallbackName);
document.write(empty);
document.close();
frame.remove();
};
// Write to the document
document.write([
'<html><head><script src="',
finalUrl,'" onload="',
errorCallbackName,'()" onreadystatechange="',
errorCallbackName,'(this.readyState)"></script></head><body onload="',
errorCallbackName,'()"></body></html>'
].join(empty)
);
// Close (makes some browsers happier)
document.close();
// If a timeout is needed, install it
timeout>0 && (timeoutTimer = setTimeout(function(){
!done && errorFunction(empty,"timeout");
},timeout));
});
return xOptions;
}
// ###################### SETUP FUNCTION ##
jsonp.setup = function(xOptions) {
$.extend(xOptionsDefaults,xOptions);
};
// ###################### INSTALL in jQuery ##
$.jsonp = jsonp;
})(jQuery);