2010-12-09 18:34:28 +00:00
|
|
|
(function( jQuery ) {
|
|
|
|
|
|
|
|
var jsc = jQuery.now(),
|
2011-01-12 17:36:00 +00:00
|
|
|
jsre = /(\=)(?:\?|%3F)(&|$)|()(?:\?\?|%3F%3F)()/i,
|
2010-12-11 00:35:46 +00:00
|
|
|
rquery_jsonp = /\?/;
|
2010-12-09 18:34:28 +00:00
|
|
|
|
2011-01-09 20:19:27 +00:00
|
|
|
// Default jsonp settings
|
|
|
|
jQuery.ajaxSetup({
|
|
|
|
jsonp: "callback",
|
|
|
|
jsonpCallback: function() {
|
|
|
|
return "jsonp" + jsc++;
|
|
|
|
}
|
2010-12-09 18:34:28 +00:00
|
|
|
|
2011-01-16 01:57:39 +00:00
|
|
|
// Detect, normalize options and install callbacks for jsonp requests
|
2011-01-13 17:33:24 +00:00
|
|
|
}).ajaxPrefilter("json jsonp", function(s, originalSettings) {
|
2011-01-05 21:41:23 +00:00
|
|
|
|
2011-01-09 20:19:27 +00:00
|
|
|
if ( s.dataTypes[ 0 ] === "jsonp" ||
|
|
|
|
originalSettings.jsonp ||
|
|
|
|
originalSettings.jsonpCallback ||
|
|
|
|
jsre.test(s.url) ||
|
|
|
|
typeof(s.data) === "string" && jsre.test(s.data) ) {
|
2010-12-09 18:34:28 +00:00
|
|
|
|
2011-01-16 01:57:39 +00:00
|
|
|
var responseContainer,
|
|
|
|
jsonpCallback = s.jsonpCallback =
|
2010-12-09 18:34:28 +00:00
|
|
|
jQuery.isFunction( s.jsonpCallback ) ? s.jsonpCallback() : s.jsonpCallback,
|
2011-01-16 01:57:39 +00:00
|
|
|
previous = window[ jsonpCallback ],
|
2011-01-12 17:36:00 +00:00
|
|
|
url = s.url.replace(jsre, "$1" + jsonpCallback + "$2"),
|
|
|
|
data = s.url === url && typeof(s.data) === "string" ? s.data.replace(jsre, "$1" + jsonpCallback + "$2") : s.data;
|
2011-01-05 21:41:23 +00:00
|
|
|
|
2011-01-09 20:19:27 +00:00
|
|
|
if ( url === s.url && data === s.data ) {
|
|
|
|
url += (rquery_jsonp.test( url ) ? "&" : "?") + s.jsonp + "=" + jsonpCallback;
|
2010-12-09 18:34:28 +00:00
|
|
|
}
|
2011-01-05 21:41:23 +00:00
|
|
|
|
2010-12-09 18:34:28 +00:00
|
|
|
s.url = url;
|
|
|
|
s.data = data;
|
2011-01-05 21:41:23 +00:00
|
|
|
|
2011-01-16 01:57:39 +00:00
|
|
|
window [ jsonpCallback ] = function( response ) {
|
|
|
|
responseContainer = [response];
|
|
|
|
};
|
2011-01-05 21:41:23 +00:00
|
|
|
|
2011-01-16 01:57:39 +00:00
|
|
|
s.complete = [function() {
|
2011-01-05 21:41:23 +00:00
|
|
|
|
2011-01-16 01:57:39 +00:00
|
|
|
// Set callback back to previous value
|
|
|
|
window[ jsonpCallback ] = previous;
|
2010-12-09 18:34:28 +00:00
|
|
|
|
2011-01-16 01:57:39 +00:00
|
|
|
// Call if it was a function and we have a response
|
|
|
|
if ( previous) {
|
|
|
|
if ( responseContainer && jQuery.isFunction ( previous ) ) {
|
|
|
|
window[ jsonpCallback ] ( responseContainer[0] );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// else, more memory leak avoidance
|
|
|
|
try{ delete window[ jsonpCallback ]; } catch(e){}
|
2010-12-09 18:34:28 +00:00
|
|
|
}
|
2011-01-05 21:41:23 +00:00
|
|
|
|
2011-01-16 01:57:39 +00:00
|
|
|
}, s.complete ];
|
2011-01-05 21:41:23 +00:00
|
|
|
|
2011-01-16 01:57:39 +00:00
|
|
|
// Use data converter to retrieve json after script execution
|
|
|
|
s.converters["script json"] = function() {
|
|
|
|
if ( ! responseContainer ) {
|
|
|
|
jQuery.error( jsonpCallback + " was not called" );
|
|
|
|
}
|
|
|
|
return responseContainer[ 0 ];
|
|
|
|
};
|
2011-01-05 21:41:23 +00:00
|
|
|
|
2011-01-16 01:57:39 +00:00
|
|
|
// force json dataType
|
|
|
|
s.dataTypes[ 0 ] = "json";
|
2011-01-09 20:19:27 +00:00
|
|
|
|
2011-01-16 01:57:39 +00:00
|
|
|
// Delegate to script
|
|
|
|
return "script";
|
|
|
|
}
|
2010-12-09 18:34:28 +00:00
|
|
|
});
|
|
|
|
|
2010-12-10 02:14:03 +00:00
|
|
|
})( jQuery );
|