JQuery | globalEval() method

This globalEval() Method in jQuery is used to execute some JavaScript code globally.

Syntax:

jQuery.globalEval( code [, options ] )

Parameters: The globalEval() method accepts two parameter that is mentioned above and described below:

  • code: This parameter is the JavaScript code to execute.
  • options => nonce : This parameter is the nonce attribute passed to the executed script.

Return Value: It returns the boolean value.

Below examples illustrate the use of globalEval() method in jQuery:



Example 1: In this example, the globalEval() method executes a script in the global context.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JQuery | globalEval() method</title
  
</head>
<body style="text-align:center;"
      
    <h1 style="color: green"
        GeeksForGeeks 
    </h1
      
    <h3>JQuery | globalEval() method</h3>
    <p>Execute a script in the global context.</p>
    <p id = "geeks"> </p>
    <script>
    function GFG() {
      jQuery.globalEval( "var newVar = 'Shubham Singh'" );
      document.getElementById("geeks").innerHTML = newVar;
    }
    GFG();
    </script>
</body>
</html>                                                                                                    

chevron_right


Output:

Example 2: In this example, the globalEval() method executes a script with a nonce value on a site with Content Security Policy enabled.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JQuery | globalEval() method</title
  
</head>
<body style="text-align:center;"
      
    <h1 style="color: green"
        GeeksForGeeks 
    </h1
      
    <h3>JQuery | globalEval() method</h3>
    <p>Execute a script with a nonce value on a<br>
    site with Content Security Policy enabled.</p>
    <p id = "geeks"> </p>
    <script>
    function GFG() {
      jQuery.globalEval( "var variable = true;", {
    nonce: "nonce-2726c7f26c"
  } );
      document.getElementById("geeks").innerHTML = variable;
    }
    GFG();
    </script>
</body>
</html>                                                                                                                                            

chevron_right


Output:




My Personal Notes arrow_drop_up

Small things always make you to think big

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.