JavaScript.EvaluateScript
-------------------------

Evaluate a string of JavaScript code.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [JavaScriptWebKit](component_JavaScriptWebKit.md) | [5.4](newinversion54.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "JavaScript.EvaluateScript"; JavaScriptContext; Script { ; URL; startingLineNumber } ) 

**MBS**( **"JavaScript.EvaluateScript";** /\* Evaluate a string of JavaScript code. \*/  
**$JavaScriptContext**; /\* The reference number of the javascript context.e.g. $Context \*/   
**$Script**; /\* The script to evaluate. \*/   
**$URL**; /\* Optional; The source URL for this script.  
A URL for the script's source file. Used by debuggers and when reporting exceptions. This parameter is informative only: it does not change the behavior of the script. \*/   
**$startingLineNumber**) /\* Optional; An integer value specifying the script's starting line number in the file located at sourceURL. This is only used when reporting exceptions. The value is one-based, so the first line is line 1 and invalid values are clamped to 1.e.g. 1 \*/ 

### Parameters

| Parameter | Description | Example | Flags |
|---|---|---|---|
| JavaScriptContext | The reference number of the javascript context. | $Context |  |
| Script | The script to evaluate. |  |  |
| URL | The source URL for this script.   A URL for the script's source file. Used by debuggers and when reporting exceptions. This parameter is informative only: it does not change the behavior of the script. |  | Optional |
| startingLineNumber | An integer value specifying the script's starting line number in the file located at sourceURL. This is only used when reporting exceptions. The value is one-based, so the first line is line 1 and invalid values are clamped to 1. | 1 | Optional |

### Result

Returns text or error.

### Description

Evaluate a string of JavaScript code.  
  
See also [WebView.RunJavaScript](WebViewRunJavaScript.md) or [WebView.Evaluate](WebViewEvaluate.md) for use with web viewer.  
See also [JS.Evaluate](JSEvaluate.md) and [JS.EvaluateToString](JSEvaluateToString.md) to run JavaScript cross-platform without web viewer.  
  
The evaluate runs within the FileMaker process. If you cause an endless loop, FileMaker freezes and needs to be force quit, so please always include an exit condition for your loops. Also if you get JavaScript to crash, FileMaker will crash with it.   
### Examples

Set variables and use them in the script:

 Set Variable \[ $js ; Value: MBS ( "[JavaScript.New](JavaScriptNew.md)" ) \]   
Set Variable \[ $r ; Value: MBS ( "[JavaScript.SetProperty](JavaScriptSetProperty.md)"; $js ; "FirstName"; "Joe" ) \]   
Set Variable \[ $r ; Value: MBS ( "[JavaScript.SetProperty](JavaScriptSetProperty.md)"; $js ; "LastName"; "Miller" ) \]   
Set Variable \[ $r ; Value: MBS ( "JavaScript.EvaluateScript"; $js ; "FirstName + ' ' + LastName;" ) \]   
Show Custom Dialog \[ "Result" ; $r \]   
Set Variable \[ $r ; Value: MBS ( "[JavaScript.Release](JavaScriptRelease.md)"; $js ) \]  
Evaluate in javascript the FileMaker get function:

 Set Variable \[ $js ; Value: MBS ( "[JavaScript.New](JavaScriptNew.md)" ) \]   
Set Variable \[ $r ; Value: MBS ( "[JavaScript.RegisterEvaluateFunction](JavaScriptRegisterEvaluateFunction.md)"; $js ; "FMEvaluate" ) \]   
Set Variable \[ $r ; Value: MBS ( "JavaScript.EvaluateScript"; $js ; "FMEvaluate('Get(AccountName)') + ' from javascript'") \]   
Show Custom Dialog \[ "Result" ; $r \]   
Set Variable \[ $r ; Value: MBS ( "[JavaScript.Release](JavaScriptRelease.md)"; $js ) \]  
Defines and runs a function in JavaScript:

 Let(\[  
f = "  
function makeCRCTable(){  
 var c;  
 var crcTable = \[\];  
 for(var n =0; n &lt; 256; n++){  
 c = n;  
 for(var k =0; k &lt; 8; k++){  
 c = ((c&amp;1) ? (0xEDB88320 ^ (c &gt;&gt;&gt; 1)) : (c &gt;&gt;&gt; 1));  
 }  
 crcTable\[n\] = c;  
 }  
 return crcTable;  
}  
  
function crc32(str) {  
 var crcTable = crcTable || (crcTable = makeCRCTable());  
 var crc = 0 ^ (-1);  
  
 for (var i = 0; i &lt; str.length; i++ ) {  
 crc = (crc &gt;&gt;&gt; 8) ^ crcTable\[(crc ^ str.charCodeAt(i)) &amp; 0xFF\];  
 }  
  
 return (crc ^ (-1)) &gt;&gt;&gt; 0;  
};  
  
crc32(InputValue);";  
j = MBS ( "[JavaScript.New](JavaScriptNew.md)" );  
r = MBS ( "[JavaScript.SetProperty](JavaScriptSetProperty.md)"; j; "InputValue"; "Hello World" );  
r = MBS ( "JavaScript.EvaluateScript"; j; f );  
x = MBS ( "[JavaScript.Release](JavaScriptRelease.md)"; j )  
\];r)  
Use trimStart(), a feature from ECMAScript 2019:

 Set Variable \[ $js ; Value: MBS ("[JavaScript.New](JavaScriptNew.md)") \]   
Set Variable \[ $r ; Value: MBS ("JavaScript.evaluateScript"; $js ; "const greeting = ' Hello world! '; greeting.trimStart();") \]   
Set Variable \[ $r ; Value: MBS ("[JavaScript.Release](JavaScriptRelease.md)"; $js ) \]  
### See also

- [JavaScript.New](JavaScriptNew.md)
- [JavaScript.RegisterEvaluateFunction](JavaScriptRegisterEvaluateFunction.md)
- [JavaScript.Release](JavaScriptRelease.md)
- [JavaScript.SetProperty](JavaScriptSetProperty.md)
- [JS.Evaluate](JSEvaluate.md)
- [JS.EvaluateToString](JSEvaluateToString.md)
- [WebView.Evaluate](WebViewEvaluate.md)
- [WebView.RunJavaScript](WebViewRunJavaScript.md)

### Example Databases

- [JavaScript/RichTextToJSON](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/JavaScript/RichTextToJSON.shtml#7CustomFunctionAnchor_)

### Blog Entries

- [Using JavaScript with WebKit on FileMaker Server on Linux](https://www.mbsplugins.de/archive/2023-03-05/Using_JavaScript_with_WebKit_o/monkeybreadsoftware_blog_filemaker)

This function checks for a license.

Created 5th November 2015 , last changed 8th July 2024

  
[JavaScript.CheckScriptSyntax](JavaScriptCheckScriptSyntax.md) - [JavaScript.GetProperty](JavaScriptGetProperty.md)

[HTML Version](JavaScriptEvaluateScript.shtml)