jQuery.globalEval()


jQuery.globalEval( code )Returns: Anything

Description: Execute some JavaScript code globally.

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).

Examples:

Execute a script in the global context.

1
2
3
4
5
function test() {
jQuery.globalEval( "var newVar = true;" );
}
test();
// newVar === true

Execute a script with a nonce value on a site with Content Security Policy enabled.

1
2
3
4
5
6
7
function test() {
jQuery.globalEval( "var newVar = true;", {
nonce: "nonce-2726c7f26c"
} );
}
test();
// newVar === true