Rewrite of globalEval. Uses window.execScript or window.eval with a trick to ensure proper context. Unit tests added.
This commit is contained in:
parent
4552d135f4
commit
f3c6077da0
25
src/core.js
25
src/core.js
|
@ -561,24 +561,17 @@ jQuery.extend({
|
||||||
|
|
||||||
noop: function() {},
|
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 ) {
|
globalEval: function( data ) {
|
||||||
if ( data && rnotwhite.test( data ) ) {
|
if ( data && rnotwhite.test( data ) ) {
|
||||||
// Inspired by code by Andrea Giammarchi
|
// We use execScript on Internet Explorer
|
||||||
// http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
|
// We use an anonymous function so that context is window
|
||||||
var head = document.head || document.getElementsByTagName( "head" )[0] || document.documentElement,
|
// rather than jQuery in Firefox
|
||||||
script = document.createElement( "script" );
|
( window.execScript || function( data ) {
|
||||||
|
window[ "eval" ].call( window, data );
|
||||||
if ( jQuery.support.scriptEval() ) {
|
} )( data );
|
||||||
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 );
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -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 ) {
|
if ( !isLocal ) {
|
||||||
test("browser", function() {
|
test("browser", function() {
|
||||||
stop();
|
stop();
|
||||||
|
|
Loading…
Reference in a new issue