jQuery globalEval() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Utilities

Definition

jQuery.globalEval(code) (alias $.globalEval) executes a JavaScript string in the global scope (since jQuery 1.0.4). Unlike eval(), declarations become true globals — important when loading scripts dynamically.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$.globalEval(code)                    // jQuery 1.0.4+
$.globalEval(code, { nonce: "..." })  // jQuery 3.4+ (CSP)
$.globalEval(code, options, doc)      // jQuery 3.5+ (iframe doc)

function test() {
  $.globalEval("var newVar = true;");
}
test();
// newVar === true  (global!)
CallWhat it does
globalEval()Learn jQuery.globalEval(): run a JavaScript string in the global scope, compare with eval(), CSP nonce option, iframe doc parameter, security warnings, examples, pitfalls, and FAQs.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Start with a trusted JavaScript string (never user input).
Step 2Call $.globalEval(code) so var / function declarations land on the global object.
Step 3On CSP sites (jQuery 3.4+), pass { nonce: "..." } as the second argument.

Return Value

Return ValueDescription
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

Example 1 — Official Global Flag

Create a global variable from inside a function.

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

console.log(typeof newVar);  // "boolean"
console.log(newVar);         // true
Try It Yourself

How It Works

  1. Call from a function. test() shows the code is not running at top level.
  2. Run globally. jQuery.globalEval("var newVar = true;") defines newVar on the global object.
  3. After return. The variable still exists — it is not local to test.

Example 2 — globalEval vs eval()

Same string, different scope rules.

jQuery
function demo() {
  $.globalEval("var fromGlobalEval = 'global';");
  eval("var fromEval = 'local';");
}
demo();

console.log(typeof fromGlobalEval);  // "string"
console.log(typeof fromEval);        // "undefined"
Try It Yourself

How It Works

  1. globalEval. var fromGlobalEval is attached to the global object.
  2. eval. var fromEval stays inside demo() and disappears when the function ends.
  3. Check types. After demo(), only fromGlobalEval exists.

Example 3 — Multi-Statement Global Helper

Define a small counter API from one script string.

jQuery
var script = [
  "window.demoCounter = 0;",
  "window.incrementDemo = function () {",
  "  window.demoCounter++;",
  "  return window.demoCounter;",
  "};"
].join("\n");

$.globalEval(script);

console.log(incrementDemo());  // 1
console.log(incrementDemo());  // 2
Try It Yourself

How It Works

  1. Build the string. Several statements joined with newlines — still one code argument.
  2. Attach to window. Counter and function are explicit globals.
  3. Evaluate once. $.globalEval(script) installs both names.

Frequently Asked Questions

jQuery.globalEval(code) executes a string of JavaScript in the global scope — the same context as scripts in the page. Variables and functions declared with var or function in that string become globals (on window in browsers).
eval() runs in the current lexical scope, so var declarations inside a function stay local. globalEval() always runs in the global context, which matters when dynamically loading scripts that must define window-level APIs.
jQuery uses it when processing inline script tags inserted through DOM APIs — for example .html() or .append() with script blocks — so those scripts run globally like normal page scripts.
No. Never pass untrusted strings — it runs arbitrary code and enables XSS. Only use trusted, server-controlled script content. Prefer $.getScript() for remote files or ES modules for modern apps.
Since jQuery 3.4, pass { nonce: "..." } as the second argument so the executed script carries a CSP nonce — required on sites with a strict Content Security Policy for inline scripts.
jQuery.globalEval(code, options, doc) accepts a Document so code runs in that document's global context — useful for scripts inside iframes where the top-level window is the wrong scope.
No. It targets a browser document (and optional iframe Document). Use Node's own module system or vm APIs on the server — not jQuery.globalEval.

Next: jQuery.queue() Method

See why jQuery’s internal feature flags should not drive app code.

jQuery.queue() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful