You are here: Reference > JavaScript > client-side > HTML DOM > methods > execScript (window)

execScript method (window)

Browser support:
Executes the specified script in the specified language.
This method can be useful if you want to execute a script written in another scripting language from JavaScript, but it can also be useful when you have a JavaScript code as a string.
If you need to execute a string of JavaScript code, use the Function object or the eval method for cross-browser functionality.

Syntax:

object.execScript (code, language);
You can find the related objects in the Supported by objects section below.

Parameters:

code
Required. String that specifies the code to be executed.
language
Required. String that specifies the scripting language. This parameter is not case sensitive.
One of the following values:
JavaScript
Default in Google Chrome. The language is JavaScript.
JavaScript1.1
The language is JavaScript 1.1.
JavaScript1.2
The language is JavaScript 1.2.
JavaScript1.3
The language is JavaScript 1.3.
JScript
Default in Internet Explorer. The language is Microsoft JScript.
VBS
Same as VBScript
VBScript
The language is Microsoft Visual Basic Scripting Edition.

Return value:

Always returns null.

Example HTML code 1:

This example illustrates the use of the execScript method:
<head>
    <script type="text/javascript">
        var jsCode = "alert ('Execution is completed.');";
        if (window.execScript) {        // Internet Explorer and Google Chrome
            execScript (jsCode, "JavaScript");
        }
        else {
            if (window.eval) {
                // the script language  is always JavaScript for the eval method
                eval (jsCode);
            }
        }
    </script>
</head>
Did you find this example helpful? yes no

Example HTML code 2:

This example is the same as the previous one, but is uses the cross-browser Function object to execute a string of JavaScript code:
<head>
    <script type="text/javascript">
        var jsCode = "alert ('Execution is completed.');";
        (new Function (jsCode)) ();
    </script>
</head>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content