3 var jsc = jQuery.now(),
4 jsre = /\=(?:\?|%3F)(&|$)/i,
7 // Default jsonp settings
10 jsonpCallback: function() {
11 return "jsonp" + jsc++;
15 // Normalize jsonp queries
16 // 1) put callback parameter in url or data
17 // 2) sneakily ensure transportDataType is always jsonp for jsonp requests
18 jQuery.ajax.prefilter("json jsonp", function(s, originalSettings) {
20 if ( s.dataTypes[ 0 ] === "jsonp" ||
21 originalSettings.jsonp ||
22 originalSettings.jsonpCallback ||
24 typeof(s.data) === "string" && jsre.test(s.data) ) {
26 var jsonpCallback = s.jsonpCallback =
27 jQuery.isFunction( s.jsonpCallback ) ? s.jsonpCallback() : s.jsonpCallback,
28 url = s.url.replace(jsre, "=" + jsonpCallback + "$1"),
29 data = s.url === url && typeof(s.data) === "string" ? s.data.replace(jsre, "=" + jsonpCallback + "$1") : s.data;
31 if ( url === s.url && data === s.data ) {
32 url += (rquery_jsonp.test( url ) ? "&" : "?") + s.jsonp + "=" + jsonpCallback;
37 s.dataTypes[ 0 ] = "jsonp";
40 // Bind transport to jsonp dataType
41 }).transport("jsonp", function(s) {
43 // Put callback in place
44 var responseContainer,
45 jsonpCallback = s.jsonpCallback,
46 previous = window[ jsonpCallback ];
48 window [ jsonpCallback ] = function( response ) {
49 responseContainer = [response];
52 s.complete = [function() {
54 // Set callback back to previous value
55 window[ jsonpCallback ] = previous;
57 // Call if it was a function and we have a response
59 if ( responseContainer && jQuery.isFunction ( previous ) ) {
60 window[ jsonpCallback ] ( responseContainer[0] );
63 // else, more memory leak avoidance
64 try{ delete window[ jsonpCallback ]; } catch(e){}
69 // Sneakily ensure this will be handled as json
70 s.dataTypes[ 0 ] = "json";
72 // Use data converter to retrieve json after script execution
73 s.converters["script json"] = function() {
74 if ( ! responseContainer ) {
75 jQuery.error( jsonpCallback + " was not called" );
77 return responseContainer[ 0 ];
80 // Delegate to script transport