Components All New MacOS Windows Linux iOS
Examples Mac & Win Server Client Guides Statistic FMM Blog Deprecated Old

JavaScript.EvaluateScript

Evaluate a string of JavaScript code.

Component Version macOS Windows Linux Server iOS SDK
JavaScriptWebKit 5.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes ✅ Yes
MBS( "JavaScript.EvaluateScript"; JavaScriptContext; Script { ; URL; startingLineNumber } )   More

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 or WebView.Evaluate for use with web viewer.
See also JS.Evaluate and JS.EvaluateToString 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" ) ]
Set Variable [ $r ; Value: MBS( "JavaScript.SetProperty"; $js; "FirstName"; "Joe" ) ]
Set Variable [ $r ; Value: MBS( "JavaScript.SetProperty"; $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"; $js ) ]

Evaluate in javascript the FileMaker get function:

Set Variable [ $js ; Value: MBS( "JavaScript.New" ) ]
Set Variable [ $r ; Value: MBS( "JavaScript.RegisterEvaluateFunction"; $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"; $js ) ]

Defines and runs a function in JavaScript:

Let([
f = "
function makeCRCTable(){
        var c;
        var crcTable = [];
        for(var n =0; n < 256; n++){
                c = n;
                for(var k =0; k < 8; k++){
                        c = ((c&1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
                }
                crcTable[n] = c;
        }
        return crcTable;
}

function crc32(str) {
        var crcTable = crcTable || (crcTable = makeCRCTable());
        var crc = 0 ^ (-1);

        for (var i = 0; i < str.length; i++ ) {
                crc = (crc >>> 8) ^ crcTable[(crc ^ str.charCodeAt(i)) & 0xFF];
        }

        return (crc ^ (-1)) >>> 0;
};

crc32(InputValue);";
j = MBS( "JavaScript.New" );
r = MBS( "JavaScript.SetProperty"; j; "InputValue"; "Hello World" );
r = MBS( "JavaScript.EvaluateScript"; j; f );
x = MBS( "JavaScript.Release"; j )
];r)

Use trimStart(), a feature from ECMAScript 2019:

Set Variable [ $js ; Value: MBS("JavaScript.New") ]
Set Variable [ $r ; Value: MBS("JavaScript.evaluateScript"; $js; "const greeting = ' Hello world! '; greeting.trimStart();") ]
Set Variable [ $r ; Value: MBS("JavaScript.Release"; $js) ]

See also

Example Databases

Blog Entries

This function checks for a license.

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


JavaScript.CheckScriptSyntax - JavaScript.GetProperty