]> source.dussan.org Git - jquery.git/commitdiff
Rewrite of globalEval. Uses window.execScript or window.eval with a trick to ensure...
authorjaubourg <j@ubourg.net>
Thu, 7 Apr 2011 04:47:15 +0000 (06:47 +0200)
committerjaubourg <j@ubourg.net>
Thu, 7 Apr 2011 04:47:15 +0000 (06:47 +0200)
src/core.js
test/unit/core.js

index a893fc9b3382b0d046bb061d9cd40acae431b6b5..7f63012a3bedf11173439866821c319fbdc55d32 100644 (file)
@@ -561,24 +561,17 @@ jQuery.extend({
 
        noop: function() {},
 
-       // Evalulates a script in a global context
+       // Evaluates a script in a global context
+       // Workarounds based on findings by Jim Driscoll
+       // http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context
        globalEval: function( data ) {
-               if ( data && rnotwhite.test(data) ) {
-                       // Inspired by code by Andrea Giammarchi
-                       // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
-                       var head = document.head || document.getElementsByTagName( "head" )[0] || document.documentElement,
-                               script = document.createElement( "script" );
-
-                       if ( jQuery.support.scriptEval() ) {
-                               script.appendChild( document.createTextNode( data ) );
-                       } else {
-                               script.text = data;
-                       }
-
-                       // Use insertBefore instead of appendChild to circumvent an IE6 bug.
-                       // This arises when a base node is used (#2709).
-                       head.insertBefore( script, head.firstChild );
-                       head.removeChild( script );
+               if ( data && rnotwhite.test( data ) ) {
+                       // We use execScript on Internet Explorer
+                       // We use an anonymous function so that context is window
+                       // rather than jQuery in Firefox
+                       ( window.execScript || function( data ) {
+                               window[ "eval" ].call( window, data );
+                       } )( data );
                }
        },
 
index 03325ab77a31a14fc751731bcdd32d0610ce7c23..79710025e683b27b8e710656eaf7d655c4580a6b 100644 (file)
@@ -172,6 +172,26 @@ test("selector state", function() {
        );
 });
 
+test( "globalEval", function() {
+
+       expect( 3 );
+
+       jQuery.globalEval( "var globalEvalTest = true;" );
+       ok( window.globalEvalTest, "Test variable declarations are global" );
+
+       window.globalEvalTest = false;
+
+       jQuery.globalEval( "globalEvalTest = true;" );
+       ok( window.globalEvalTest, "Test variable assignments are global" );
+
+       window.globalEvalTest = false;
+
+       jQuery.globalEval( "this.globalEvalTest = true;" );
+       ok( window.globalEvalTest, "Test context (this) is the window object" );
+
+       window.globalEvalTest = undefined;
+});
+
 if ( !isLocal ) {
 test("browser", function() {
        stop();