Topics   All   MacOS (Only)   Windows (Only)   Linux (Only, Not)   iOS (Only, Not)  
Components   Crossplatform Mac & Win   Server   Client   Old   Deprecated   Guides   Examples   Videos
New in version: 9.1   9.2   9.3   9.4   9.5   10.0   10.1   10.2   10.3   10.4    Statistic  

WebView.Evaluate

Evaluates a JavaScript expression.

Component Version macOS Windows Server FileMaker Cloud FileMaker iOS SDK
Webview 10.0 Yes Yes No No Yes
MBS( "WebView.Evaluate"; WebViewerRef; Expression )   More

Parameters

Parameter Description Example value
WebViewerRef Either the Web Viewer Object Name or the Web Viewer ID as returned by "WebView.FindByName" function.
Expression The JavaScript expression to evaluate. "1+"

Result

Returns value or error.

Description

Evaluates a JavaScript expression.
In contrast to WebView.RunJavaScript this function returns the result on Windows, but needs IE 9 or newer.
Use WebView.SetInternetExplorerVersion to configure the IE version used in web viewer.

For MacOS and iOS this passes through to WebView.RunJavaScript function.

Returns result on MacOS/iOS if possible in original data type, so numbers and booleans are returned as number and not as text. We try to return array or objects as JSON.
On Windows booleans and numbers are returned as numbers.

Examples

Evaluate an expression:

MBS( "WebView.Evaluate"; "web"; "1+2" )

Build some JavaScript object and return as JSON:

MBS("WebView.Evaluate"; "web"; "var obj = { name: \"John\", age: 30, city: \"New York\" };
JSON.stringify(obj);")

Defines and runs a function in JavaScript in the web viewer:

MBS("WebView.Evaluate"; "web";
"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('Hello World');")

Fill a search form, submit and query result:

# load website with search field:
Set Web Viewer [ Object Name: "web" ; URL: "https://something.com/searchpage" ]
#
# wait for website to finish loading
Pause/Resume Script [ Duration (seconds): ,5 ]
Loop
    Exit Loop If [ MBS("WebView.IsLoading"; "web") ≠ 1 ]
    Pause/Resume Script [ Duration (seconds): ,1 ]
End Loop
Pause/Resume Script [ Duration (seconds): ,5 ]
#
# fill in search term and send change event, so this website sees it:
Set Variable [ $r ; Value: MBS("WebView.RunJavaScript"; "web"; "document.getElementById('searchField').value = '" & Substitute ( Web::SearchTerm ; "'" ; "" ) & "'") ]
Set Variable [ $r ; Value: MBS("WebView.RunJavaScript"; "web"; "var o = document.getElementById('searchField'); var evt = document.createEvent('Events'); evt.initEvent('change', true, true); o.dispatchEvent(evt); }") ]
#
# wait a bit
Pause/Resume Script [ Duration (seconds): ,5 ]
#
# send click event to search button
Set Variable [ $r ; Value: MBS("WebView.RunJavaScript"; "web"; "var o = document.getElementById('searchButton'); var evt = document.createEvent('Events'); evt.initEvent('click', true, true); o.dispatchEvent(evt); ") ]
#
# wait for website to finish loading
Pause/Resume Script [ Duration (seconds): ,5 ]
Loop
    Exit Loop If [ MBS("WebView.IsLoading"; "web") ≠ 1 ]
    Pause/Resume Script [ Duration (seconds): ,1 ]
End Loop
Pause/Resume Script [ Duration (seconds): ,5 ]
#
# query values from website
Set Field [ Web::articleId ; MBS("WebView.Evaluate"; "web"; "document.getElementById('articleId').innerText;") ]
Set Field [ Web::price ; MBS("WebView.Evaluate"; "web"; "document.getElementById('price').innerText;") ]

Wait for field named Remember to show up on website:

Pause/Resume Script [ Duration (seconds): 1 ]
Set Variable [ $count ; Value: 0 ]
Loop
    Set Variable [ $r ; Value: MBS("WebView.Evaluate"; "web"; "(null == document.getElementById('Remember'))") ]
    Exit Loop If [ $r = 0 ]
    Pause/Resume Script [ Duration (seconds): 1 ]
    Set Variable [ $count ; Value: $count + 1 ]
    If [ $count = 20 ]
        Show Custom Dialog [ "Timeout" ; "Failed to load website and get the remember checkbox." ]
        Exit Script [ Text Result: ]
    End If
End Loop
Pause/Resume Script [ Duration (seconds): 1 ]
# now you can set/query it

Query scroll position via JavaScript:

MBS( "WebView.Evaluate"; "web"; "document.scrollingElement.scrollTop" )

Run FileMaker's message handler in FileMaker 19:

MBS( "WebView.Evaluate"; "web"; "if (typeof(FileMaker) != 'undefined') { FileMaker.PerformScript('test', 'Hello'); r = 'OK'; } else { r = 'Failed'; }; r;")
// returns OK on success or Failed if not.

Check if FileMaker 19 message handler is installed:

MBS( "WebView.Evaluate"; "web"; "typeof(FileMaker)")
// returns "object" if installed or "undefined"

See also

Example Databases

Blog Entries

Release notes

  • Version 10.0
    • Added WebView.Evaluate function to evaluate JavaScript expression and get back result for Windows.

Created 4th January 2020, last changed 4th June 2020


WebView.EstimatedProgress   -   WebView.ExecCommand

Feedback: Report problem or ask question.




Links
MBS Xojo PDF Plugins