Skip to content

Commit 37f0f7f

Browse files
committed
Ajax: Always use script injection in globalEval
Fixes #14757 Ref bbdfbb4
1 parent 76294e1 commit 37f0f7f

File tree

6 files changed

+62
-44
lines changed

6 files changed

+62
-44
lines changed

src/core.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -275,17 +275,18 @@ jQuery.extend({
275275
},
276276

277277
// Evaluates a script in a global context
278-
// Workarounds based on findings by Jim Driscoll
279-
// http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context
280278
globalEval: function( data ) {
281-
if ( data && jQuery.trim( data ) ) {
282-
// We use execScript on Internet Explorer
283-
// We use an anonymous function so that context is window
284-
// rather than jQuery in Firefox
285-
( window.execScript || function( data ) {
286-
window[ "eval" ].call( window, data );
287-
} )( data );
288-
}
279+
// Inspired by code by Andrea Giammarchi
280+
// http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
281+
var head = document.head || jQuery( "head" )[ 0 ] || document.documentElement,
282+
script = document.createElement( "script" );
283+
284+
script.text = data;
285+
286+
// Support: IE6
287+
// Circumvent bugs with base elements (#2709 and #4378) by prepending
288+
head.insertBefore( script, head.firstChild );
289+
head.removeChild( script );
289290
},
290291

291292
// Convert dashed to camelCase; used by the css and data modules

test/data/event/syncReady.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<body>
99

1010
<script type="text/javascript">
11-
jQuery( document ).ready(function () {
11+
jQuery( document ).ready(function() {
1212
window.parent.iframeCallback( jQuery('#container').length === 1 );
1313
});
1414
</script>
@@ -17,7 +17,7 @@
1717
oldIE into thinking the dom is ready, but it's not...
1818
leaving this check here for future trailblazers to attempt
1919
fixing this...-->
20-
<script type="text/javascript" src="longLoadScript.php?sleep=1"></script>
20+
<script type="text/javascript" src="../longLoadScript.php?sleep=1"></script>
2121
<div id="container" style="height: 300px"></div>
2222
</body>
2323
</html>

test/unit/ajax.js

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,24 +1468,18 @@ module( "ajax", {
14681468
}
14691469
});
14701470

1471-
test( "#11743 - jQuery.ajax() - script, throws exception", 1, function() {
1472-
throws(function() {
1473-
jQuery.ajax({
1474-
url: "data/badjson.js",
1475-
dataType: "script",
1476-
"throws": true,
1477-
// TODO find a way to test this asynchronously, too
1478-
async: false,
1479-
// Global events get confused by the exception
1480-
global: false,
1481-
success: function() {
1482-
ok( false, "Success." );
1483-
},
1484-
error: function() {
1485-
ok( false, "Error." );
1486-
}
1487-
});
1488-
}, "exception bubbled" );
1471+
asyncTest( "#11743 - jQuery.ajax() - script, throws exception", 1, function() {
1472+
var onerror = window.onerror;
1473+
window.onerror = function() {
1474+
ok( true, "Exception thrown" );
1475+
window.onerror = onerror;
1476+
start();
1477+
};
1478+
jQuery.ajax({
1479+
url: "data/badjson.js",
1480+
dataType: "script",
1481+
"throws": true
1482+
});
14891483
});
14901484

14911485
jQuery.each( [ "method", "type" ], function( _, globalOption ) {

test/unit/core.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,19 @@ test( "globalEval", function() {
214214
equal( window.globalEvalTest, 3, "Test context (this) is the window object" );
215215
});
216216

217+
test( "globalEval execution after script injection (#7862)", 1, function() {
218+
var now,
219+
script = document.createElement( "script" );
220+
221+
script.src = "data/longLoadScript.php?sleep=2";
222+
223+
now = jQuery.now();
224+
document.body.appendChild( script );
225+
226+
jQuery.globalEval( "var strictEvalTest = " + jQuery.now() + ";");
227+
ok( window.strictEvalTest - now < 500, "Code executed synchronously" );
228+
});
229+
217230
test("noConflict", function() {
218231
expect(7);
219232

test/unit/manipulation.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2263,23 +2263,33 @@ test( "Ensure oldIE creates a new set on appendTo (#8894)", function() {
22632263
strictEqual( jQuery("<p/>").appendTo("<div/>").end().length, jQuery("<p>test</p>").appendTo("<div/>").end().length, "Elements created with createElement and with createDocumentFragment should be treated alike" );
22642264
});
22652265

2266-
test( "html() - script exceptions bubble (#11743)", function() {
2266+
asyncTest( "html() - script exceptions bubble (#11743)", 2, function() {
22672267

2268-
expect( 2 );
2268+
var onerror = window.onerror;
22692269

2270-
throws(function() {
2271-
jQuery("#qunit-fixture").html("<script>undefined(); ok( false, 'Exception not thrown' );</script>");
2272-
ok( false, "Exception ignored" );
2273-
}, "Exception bubbled from inline script" );
2270+
setTimeout(function() {
2271+
window.onerror = onerror;
22742272

2275-
if ( jQuery.ajax ) {
2276-
throws(function() {
2277-
jQuery("#qunit-fixture").html("<script src='data/badcall.js'></script>");
2278-
ok( false, "Exception ignored" );
2279-
}, "Exception thrown in remote script" );
2280-
} else {
2281-
ok( true, "No jQuery.ajax" );
2282-
}
2273+
start();
2274+
}, 1000 );
2275+
2276+
window.onerror = function() {
2277+
ok( true, "Exception thrown" );
2278+
2279+
if ( jQuery.ajax ) {
2280+
window.onerror = function() {
2281+
ok( true, "Exception thrown in remote script" );
2282+
};
2283+
2284+
jQuery( "#qunit-fixture" ).html( "<script src='data/badcall.js'></script>" );
2285+
ok( true, "Exception ignored" );
2286+
} else {
2287+
ok( true, "No jQuery.ajax" );
2288+
ok( true, "No jQuery.ajax" );
2289+
}
2290+
};
2291+
2292+
jQuery( "#qunit-fixture" ).html( "<script>undefined();</script>" );
22832293
});
22842294

22852295
test( "checked state is cloned with clone()", function() {

0 commit comments

Comments
 (0)