jQuery.globalEval()

Execute some JavaScript code globally.

jQuery.globalEval(code)👒 Anything

code String The JavaScript code to execute.

jQuery.globalEval(code, options)👒 Anything

code String The JavaScript code to execute.
options PlainObject undefined
nonce string The nonce attribute passed to the executed script.

jQuery.globalEval(code, options, doc)👒 Anything

code String The JavaScript code to execute.
options PlainObject undefined
nonce string The nonce attribute passed to the executed script.
doc Document A document in which context the code will be evaluated.

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

Execute a script in the global context.

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

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

HTML
function test() {
 jQuery.globalEval("var newVar = true;", {
 nonce: "nonce-2726c7f26c",
 });
}
test();
// newVar === true
DEMO

Looking for a Web Developer?

πŸ‘‹

Hi! I'm Basti, author of this site. If you are looking for a web developer with 15+ years of experience, holla at me!

Be it the good 'ol jQuery, vanilla JS or modern frameworks like Vue and Svelte, front- or backend, I can help you.

Just write me at jobs@jqapi.com :)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /