aboutsummaryrefslogtreecommitdiffstats
path: root/src/ajax/jsonp.js
diff options
context:
space:
mode:
authorjaubourg <j@ubourg.net>2012-04-20 03:02:20 +0200
committerjaubourg <j@ubourg.net>2012-04-20 03:02:20 +0200
commit8ebb2f4793d2c464888b5f0dba97fb5c9d0c9541 (patch)
treed6bb719534c33cb22bdb88aee0a73a0730facec3 /src/ajax/jsonp.js
parent3e6f94c36046e927121e2b25174f1ecd2e45d65b (diff)
downloadjquery-8ebb2f4793d2c464888b5f0dba97fb5c9d0c9541.tar.gz
jquery-8ebb2f4793d2c464888b5f0dba97fb5c9d0c9541.zip
Fixes #8205. Mitigates memory usage by recycling jsonp callback names the safest possible way (no kittens were harmed in the making of this). Doesn't even try to delete window properties (would necessitate a try/catch for IE which makes the cost in size prohibitive). Unit tests added.
Diffstat (limited to 'src/ajax/jsonp.js')
-rw-r--r--src/ajax/jsonp.js14
1 files changed, 12 insertions, 2 deletions
diff --git a/src/ajax/jsonp.js b/src/ajax/jsonp.js
index e1f2b5314..9965b2230 100644
--- a/src/ajax/jsonp.js
+++ b/src/ajax/jsonp.js
@@ -1,13 +1,16 @@
(function( jQuery ) {
var jsc = jQuery.now(),
- jsre = /(\=)\?(&|$)|\?\?/i;
+ jsre = /(\=)\?(&|$)|\?\?/i,
+ jscallbacks = [];
// Default jsonp settings
jQuery.ajaxSetup({
jsonp: "callback",
jsonpCallback: function() {
- return jQuery.expando + "_" + ( jsc++ );
+ var callback = jscallbacks.pop() || ( jQuery.expando + "_" + ( jsc++ ) );
+ this[ callback ] = true;
+ return callback;
}
});
@@ -53,6 +56,13 @@ jQuery.ajaxPrefilter( "json jsonp", function( s, originalSettings, jqXHR ) {
jqXHR.always(function() {
// Set callback back to previous value
window[ jsonpCallback ] = previous;
+ // Save back as free
+ if ( s[ jsonpCallback ] ) {
+ // make sure that re-using the options doesn't screw things around
+ s.jsonpCallback = originalSettings.jsonpCallback;
+ // save the callback name for future use
+ jscallbacks.push( jsonpCallback );
+ }
// Call if it was a function and we have a response
if ( responseContainer && jQuery.isFunction( previous ) ) {
window[ jsonpCallback ]( responseContainer[ 0 ] );