3 var jsc = jQuery.now(),
4 jsre = /(\=)(?:\?|%3F)(&|$)|()(?:\?\?|%3F%3F)()/i;
6 // Default jsonp settings
9 jsonpCallback: function() {
10 return "jsonp" + jsc++;
13 // Detect, normalize options and install callbacks for jsonp requests
14 // (dataIsString is used internally)
15 }).ajaxPrefilter("json jsonp", function(s, originalSettings, dataIsString) {
17 dataIsString = ( typeof(s.data) === "string" );
19 if ( s.dataTypes[ 0 ] === "jsonp" ||
20 originalSettings.jsonpCallback ||
21 originalSettings.jsonp != null ||
22 s.jsonp !== false && ( jsre.test( s.url ) ||
23 dataIsString && jsre.test( s.data ) ) ) {
25 var responseContainer,
26 jsonpCallback = s.jsonpCallback =
27 jQuery.isFunction( s.jsonpCallback ) ? s.jsonpCallback() : s.jsonpCallback,
28 previous = window[ jsonpCallback ],
31 replace = "$1" + jsonpCallback + "$2";
33 if ( s.jsonp !== false ) {
34 url = url.replace( jsre, replace );
35 if ( s.url === url ) {
37 data = data.replace( jsre, replace );
39 if ( s.data === data ) {
40 // Add callback manually
41 url += (/\?/.test( url ) ? "&" : "?") + s.jsonp + "=" + jsonpCallback;
49 window [ jsonpCallback ] = function( response ) {
50 responseContainer = [response];
53 s.complete = [function() {
55 // Set callback back to previous value
56 window[ jsonpCallback ] = previous;
58 // Call if it was a function and we have a response
60 if ( responseContainer && jQuery.isFunction ( previous ) ) {
61 window[ jsonpCallback ] ( responseContainer[0] );
64 // else, more memory leak avoidance
65 try{ delete window[ jsonpCallback ]; } catch(e){}
70 // Use data converter to retrieve json after script execution
71 s.converters["script json"] = function() {
72 if ( ! responseContainer ) {
73 jQuery.error( jsonpCallback + " was not called" );
75 return responseContainer[ 0 ];
78 // force json dataType
79 s.dataTypes[ 0 ] = "json";