jQuery API

jQuery.globalEval()

jQuery.globalEval( code )

Description: Execute some JavaScript code globally.

  • version added: 1.0.4jQuery.globalEval( code )

    codeThe JavaScript code to execute.

This method behaves differently from using a normal JavaScript eval() in that it's executed within the global context (which is important for loading external scripts dynamically).

Example:

Execute a script in the global context.

function test(){
    jQuery.globalEval("var newVar = true;")
}
test();
// newVar === true

Comments

  • Support requests, bug reports, and off-topic comments will be deleted without warning.

  • Please do post corrections or additional examples for jQuery.globalEval() below. We aim to quickly move corrections into the documentation.
  • If you need help, post at the forums or in the #jquery IRC channel.
  • Report bugs on the bug tracker or the jQuery Forum.
  • Discussions about the API specifically should be addressed in the Developing jQuery Core forum.
  • what is a good use case for this, doesn't this go against Doug Crockford's "eval is evil" principle.