3 var jsc = jQuery.now(),
4 jsre = /(\=)(?:\?|%3F)(&|$)|()(?:\?\?|%3F%3F)()/i;
6 // Default jsonp settings
9 jsonpCallback: function() {
10 return "jsonp" + jsc++;
14 // Detect, normalize options and install callbacks for jsonp requests
15 jQuery.ajaxPrefilter( "json jsonp", function( s, originalSettings, dataIsString /* internal */ ) {
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
66 delete window[ jsonpCallback ];
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 // force json dataType
81 s.dataTypes[ 0 ] = "json";