3 var jsc = jQuery.now(),
4 jsre = /(\=)(?:\?|%3F)(&|$)|()(?:\?\?|%3F%3F)()/i,
7 // Default jsonp settings
10 jsonpCallback: function() {
11 return "jsonp" + jsc++;
14 // Normalize jsonp queries
15 // 1) put callback parameter in url or data
16 // 2) sneakily ensure transportDataType is always jsonp for jsonp requests
17 }).ajaxPrefilter("json jsonp", function(s, originalSettings) {
19 if ( s.dataTypes[ 0 ] === "jsonp" ||
20 originalSettings.jsonp ||
21 originalSettings.jsonpCallback ||
23 typeof(s.data) === "string" && jsre.test(s.data) ) {
25 var jsonpCallback = s.jsonpCallback =
26 jQuery.isFunction( s.jsonpCallback ) ? s.jsonpCallback() : s.jsonpCallback,
27 url = s.url.replace(jsre, "$1" + jsonpCallback + "$2"),
28 data = s.url === url && typeof(s.data) === "string" ? s.data.replace(jsre, "$1" + jsonpCallback + "$2") : s.data;
30 if ( url === s.url && data === s.data ) {
31 url += (rquery_jsonp.test( url ) ? "&" : "?") + s.jsonp + "=" + jsonpCallback;
36 s.dataTypes[ 0 ] = "jsonp";
39 // Bind transport to jsonp dataType
40 }).ajaxTransport("jsonp", function(s) {
42 // Put callback in place
43 var responseContainer,
44 jsonpCallback = s.jsonpCallback,
45 previous = window[ jsonpCallback ];
47 window [ jsonpCallback ] = function( response ) {
48 responseContainer = [response];
51 s.complete = [function() {
53 // Set callback back to previous value
54 window[ jsonpCallback ] = previous;
56 // Call if it was a function and we have a response
58 if ( responseContainer && jQuery.isFunction ( previous ) ) {
59 window[ jsonpCallback ] ( responseContainer[0] );
62 // else, more memory leak avoidance
63 try{ delete window[ jsonpCallback ]; } catch(e){}
68 // Sneakily ensure this will be handled as json
69 s.dataTypes[ 0 ] = "json";
71 // Use data converter to retrieve json after script execution
72 s.converters["script json"] = function() {
73 if ( ! responseContainer ) {
74 jQuery.error( jsonpCallback + " was not called" );
76 return responseContainer[ 0 ];
79 // Delegate to script transport