Components | All | New | MacOS | Windows | Linux | iOS | ||||
Examples | Mac & Win | Server | Client | Guides | Statistic | FMM | Blog | Deprecated | Old |
WebView.Evaluate
Evaluates a JavaScript expression.
Component | Version | macOS | Windows | Linux | Server | iOS SDK |
Webview | 10.0 | ✅ Yes | ✅ Yes | ❌ No | ❌ No | ✅ Yes |
Parameters
Parameter | Description | Example |
---|---|---|
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.
For Windows with Edge browser or Web Direct, you may use the Perform JavaScript script step in FileMaker 19 instead.
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"
Query browser version:
MBS("Webview.Evaluate"; "web"; "navigator.appVersion")
Example result: "5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36 Edg/89.0.774.75"
Query html of an element on the website:
Set Variable [ $html; Value: MBS( "WebView.Evaluate"; "web"; "document.getElementById('maintop').outerHTML" ) ]
Query list of field names for a form:
MBS("WebView.Evaluate"; "web";
"var e = document.forms[0].elements;
var list = \"\";
for (i = 0; i < e.length; i++) {
var n = e[i].name;
if (n.length > 0) { list = n + String.fromCharCode(13) + list; }
}
list;")
Query current time of video in interactive container on Mac:
MBS( "WebView.Evaluate" ; "web"; "document.getElementsByTagName('video')[0].currentTime;" )
See also
- JavaScript.EvaluateScript
- WebView.FindByName
- WebView.IsLoading
- WebView.RunJavaScript
- WebView.RunJavaScriptReturnTitle
- WebView.SetInternetExplorerVersion
Release notes
- Version 11.5
- Improved error messages when running JavaScript on macOS and iOS with WebView.Evaluate (and similar) to return the JavaScript exception message.
- Version 10.5
- Added 60 second timeout to WebView.Evaluate for macOS to avoid endless loops.
- Fixed a deadlock with WebView.Evaluate waiting endless for JavaScript answer and newer macOS versions.
- Version 10.0
- Added WebView.Evaluate function to evaluate JavaScript expression and get back result for Windows.
Example Databases
- List/Sort Benchmark
- WebViewer/Custom WebView
- WebViewer/JavaScript Evaluate
- WebViewer/WebViewer YouTube
Blog Entries
- Printing a WebViewer in FileMaker
- Using PerformScript in a custom WebViewer in FileMaker
- Control interactive containers in FileMaker
- Run JavaScript synchronously in a WebViewer
- Working with Webkit Message Handlers in FileMaker
- Check out options for FileMaker JavaScript Integration
- New Examples 2020
- FileMaker and JavaScript - the perfect combination
- New in the MBS FileMaker Plugin 10.0
- Automate web viewer in FileMaker
FileMaker Magazin
This function checks for a license.
Created 4th January 2020, last changed 5th December 2023