A fix for bug #1443, where globalEval occurred asynchronously in Safari 2, provided by Andrea Giammarchi.

This commit is contained in:
John Resig 2007-10-01 20:15:20 +00:00
parent beebbf8ba4
commit ad1cef94bf
2 changed files with 20 additions and 9 deletions

View file

@ -564,15 +564,19 @@ jQuery.extend({
data = jQuery.trim( data ); data = jQuery.trim( data );
if ( data ) { if ( data ) {
if ( window.execScript ) // Inspired by code by Andrea Giammarchi
window.execScript( data ); // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
var head = document.getElementsByTagName("head")[0] || document.documentElement,
else if ( jQuery.browser.safari ) script = document.createElement("script");
// safari doesn't provide a synchronous global eval
window.setTimeout( data, 0 );
script.type = "text/javascript";
if ( jQuery.browser.msie )
script.text = data;
else else
eval.call( window, data ); script.appendChild( document.createTextNode( data ) );
head.appendChild( script );
head.removeChild( script );
} }
}, },

View file

@ -123,11 +123,18 @@ test("isFunction", function() {
}); });
}); });
var foo = false;
test("$('html')", function() { test("$('html')", function() {
expect(2); expect(4);
reset(); reset();
ok( $("<script>var foo='test';</script>")[0], "Creating a script" ); foo = false;
var s = $("<script>var foo='test';</script>")[0];
ok( s, "Creating a script" );
ok( !foo, "Make sure the script wasn't executed prematurely" );
$("body").append(s);
ok( foo, "Executing a scripts contents in the right context" );
reset(); reset();
ok( $("<link rel='stylesheet'/>")[0], "Creating a link" ); ok( $("<link rel='stylesheet'/>")[0], "Creating a link" );