883876fc0b1733ee58916def06c0d3df7385fa4e
[jquery.git] / src / ajax / jsonp.js
1 (function( jQuery ) {
2
3 var jsc = jQuery.now(),
4         jsre = /(\=)(?:\?|%3F)(&|$)|()(?:\?\?|%3F%3F)()/i;
5
6 // Default jsonp settings
7 jQuery.ajaxSetup({
8         jsonp: "callback",
9         jsonpCallback: function() {
10                 return "jsonp" + jsc++;
11         }
12
13 // Detect, normalize options and install callbacks for jsonp requests
14 // (dataIsString is used internally)
15 }).ajaxPrefilter("json jsonp", function(s, originalSettings, dataIsString) {
16
17         dataIsString = ( typeof(s.data) === "string" );
18
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 ) ) ) {
24
25                 var responseContainer,
26                         jsonpCallback = s.jsonpCallback =
27                                 jQuery.isFunction( s.jsonpCallback ) ? s.jsonpCallback() : s.jsonpCallback,
28                         previous = window[ jsonpCallback ],
29                         url = s.url,
30                         data = s.data,
31                         replace = "$1" + jsonpCallback + "$2";
32
33                 if ( s.jsonp !== false ) {
34                         url = url.replace( jsre, replace );
35                         if ( s.url === url ) {
36                                 if ( dataIsString ) {
37                                         data = data.replace( jsre, replace );
38                                 }
39                                 if ( s.data === data ) {
40                                         // Add callback manually
41                                         url += (/\?/.test( url ) ? "&" : "?") + s.jsonp + "=" + jsonpCallback;
42                                 }
43                         }
44                 }
45
46                 s.url = url;
47                 s.data = data;
48
49                 window [ jsonpCallback ] = function( response ) {
50                         responseContainer = [response];
51                 };
52
53                 s.complete = [function() {
54
55                         // Set callback back to previous value
56                         window[ jsonpCallback ] = previous;
57
58                         // Call if it was a function and we have a response
59                         if ( previous) {
60                                 if ( responseContainer && jQuery.isFunction ( previous ) ) {
61                                         window[ jsonpCallback ] ( responseContainer[0] );
62                                 }
63                         } else {
64                                 // else, more memory leak avoidance
65                                 try{ delete window[ jsonpCallback ]; } catch(e){}
66                         }
67
68                 }, s.complete ];
69
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" );
74                         }
75                         return responseContainer[ 0 ];
76                 };
77
78                 // force json dataType
79                 s.dataTypes[ 0 ] = "json";
80
81                 // Delegate to script
82                 return "script";
83         }
84 });
85
86 })( jQuery );