Spec-Zone.ru › jQuery

jQuery.globalEval()

jQuery.globalEval( code )Returns: Anything

Description: Execute some JavaScript code globally.

  • version added: 1.0.4jQuery.globalEval( code )

    • code
      Type: String
      The JavaScript code to execute.
  • version added: 3.4jQuery.globalEval( code [, options ] )

    • code
      Type: String
      The JavaScript code to execute.
    • options
      Type: PlainObject
      • nonce
        Type: string
        The nonce attribute passed to the executed script.
  • version added: 3.5jQuery.globalEval( code [, options ] [, doc ] )

    • code
      Type: String
      The JavaScript code to execute.
    • options
      Type: PlainObject
      • nonce
        Type: string
        The nonce attribute passed to the executed script.
    • doc
      Type: 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).

Examples:

Example 1

Execute a script in the global context.

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

Example 2

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

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

© The jQuery Foundation and other contributors
Licensed under the MIT License.
https://api.jquery.com/jQuery.globalEval

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API