Skip to content

Commit 3d850ed

Browse files
committed
Ajax: Remove jsonp callbacks through "jQuery#removeProp" method
Fixes gh-2323 Closes gh-2464 Ref a2ae215
1 parent 1682d36 commit 3d850ed

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

src/ajax/jsonp.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,14 @@ jQuery.ajaxPrefilter( "json jsonp", function( s, originalSettings, jqXHR ) {
6464

6565
// Clean-up function (fires after converters)
6666
jqXHR.always(function() {
67-
// Restore preexisting value
68-
window[ callbackName ] = overwritten;
67+
// If previous value didn't exist - remove it
68+
if ( overwritten === undefined ) {
69+
jQuery( window ).removeProp( callbackName );
70+
71+
// Otherwise restore preexisting value
72+
} else {
73+
window[ callbackName ] = overwritten;
74+
}
6975

7076
// Save back as free
7177
if ( s[ callbackName ] ) {

test/unit/ajax.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
var isIE8 = /msie 8\.0/i.test( window.navigator.userAgent );
2+
13
module( "ajax", {
24
setup: function() {
5+
if ( !isIE8 ) {
6+
return;
7+
}
8+
39
var jsonpCallback = this.jsonpCallback = jQuery.ajaxSettings.jsonpCallback;
410
jQuery.ajaxSettings.jsonpCallback = function() {
511
var callback = jsonpCallback.apply( this, arguments );
@@ -737,7 +743,7 @@ module( "ajax", {
737743
}
738744
]);
739745

740-
ajaxTest( "jQuery.ajax() - JSONP - Explicit callback param" + label, 9, {
746+
ajaxTest( "jQuery.ajax() - JSONP - Explicit callback param" + label, 10, {
741747
setup: function() {
742748
Globals.register("functionToCleanUp");
743749
Globals.register("XXX");
@@ -760,6 +766,11 @@ module( "ajax", {
760766
crossDomain: crossDomain,
761767
jsonpCallback: "jsonpResults",
762768
success: function( data ) {
769+
strictEqual(
770+
typeof window[ "jsonpResults" ],
771+
"function",
772+
"should not rewrite original function"
773+
);
763774
ok( data.data, "JSON results returned (GET, custom callback name)" );
764775
}
765776
}, {
@@ -1372,16 +1383,34 @@ module( "ajax", {
13721383
]);
13731384

13741385
jQuery.each( [ " - Same Domain", " - Cross Domain" ], function( crossDomain, label ) {
1375-
ajaxTest( "#8205 - jQuery.ajax() - JSONP - re-use callbacks name" + label, 2, {
1386+
ajaxTest( "#8205 - jQuery.ajax() - JSONP - re-use callbacks name" + label, 4, {
13761387
url: "data/jsonp.php",
13771388
dataType: "jsonp",
13781389
crossDomain: crossDomain,
13791390
beforeSend: function( jqXHR, s ) {
13801391
s.callback = s.jsonpCallback;
1392+
1393+
ok( this.callback in window, "JSONP callback name is in the window" );
13811394
},
13821395
success: function() {
13831396
var previous = this;
1384-
strictEqual( previous.jsonpCallback, undefined, "jsonpCallback option is set back to default in callbacks" );
1397+
1398+
strictEqual(
1399+
previous.jsonpCallback,
1400+
undefined,
1401+
"jsonpCallback option is set back to default in callbacks"
1402+
);
1403+
1404+
if ( isIE8 ) {
1405+
ok( true, "IE8 can't remove property from the window" );
1406+
1407+
} else {
1408+
ok(
1409+
!( this.callback in window ),
1410+
"JSONP callback name was removed from the window"
1411+
);
1412+
}
1413+
13851414
jQuery.ajax({
13861415
url: "data/jsonp.php",
13871416
dataType: "jsonp",

0 commit comments

Comments
 (0)