WebView.Evaluate
----------------

Evaluates a JavaScript expression.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [Webview](component_Webview.md) | [10.0](newinversion100.md) | ✅ Yes | ✅ Yes | ❌ No | ❌ No | ✅ Yes |

MBS( "WebView.Evaluate"; WebViewerRef; Expression ) 

**MBS**( **"WebView.Evaluate";** /\* Evaluates a JavaScript expression. \*/  
**$WebViewerRef**; /\* Either the Web Viewer Object Name or the Web Viewer ID as returned by "[WebView.FindByName](WebViewFindByName.md)" function. \*/   
**$Expression**) /\* The JavaScript expression to evaluate.e.g. "1+" \*/ 

### Parameters

| Parameter | Description | Example |
|---|---|---|
| WebViewerRef | Either the Web Viewer Object Name or the Web Viewer ID as returned by "[WebView.FindByName](WebViewFindByName.md)" function. |  |
| Expression | The JavaScript expression to evaluate. | "1+" |

### Result

Returns value or error.

### Description

Evaluates a JavaScript expression.  
In contrast to [WebView.RunJavaScript](WebViewRunJavaScript.md) this function returns the result on Windows, but needs IE 9 or newer.  
Use [WebView.SetInternetExplorerVersion](WebViewSetInternetExplorerVersion.md) to configure the IE version used in web viewer.  
  
For MacOS and iOS this passes through to [WebView.RunJavaScript](WebViewRunJavaScript.md) 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 &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('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](WebViewIsLoading.md)"; "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](WebViewRunJavaScript.md)"; "web"; "document.getElementById('searchField').value = '" &amp; Substitute ( Web::SearchTerm ; "'" ; "" ) &amp; "'") \]   
Set Variable \[ $r ; Value: MBS ("[WebView.RunJavaScript](WebViewRunJavaScript.md)"; "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](WebViewRunJavaScript.md)"; "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](WebViewIsLoading.md)"; "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")  
<small>  
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"</small>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 &lt; e.length; i++) {  
 var n = e\[i\].name;  
 if (n.length &gt; 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](JavaScriptEvaluateScript.md)
- [WebView.FindByName](WebViewFindByName.md)
- [WebView.IsLoading](WebViewIsLoading.md)
- [WebView.RunJavaScript](WebViewRunJavaScript.md)
- [WebView.RunJavaScriptReturnTitle](WebViewRunJavaScriptReturnTitle.md)
- [WebView.SetInternetExplorerVersion](WebViewSetInternetExplorerVersion.md)

### Release notes

- **Version 11.5**
    - Improved error messages when running JavaScript on macOS and iOS with [WebView.Evaluate](https://www.mbsplugins.eu/WebViewEvaluate.shtml) (and similar) to return the JavaScript exception message.
- **Version 10.5**
    - Added 60 second timeout to [WebView.Evaluate](https://www.mbsplugins.eu/WebViewEvaluate.shtml) for macOS to avoid endless loops.
    - Fixed a deadlock with [WebView.Evaluate](https://www.mbsplugins.eu/WebViewEvaluate.shtml) waiting endless for JavaScript answer and newer macOS versions.
- **Version 10.0**
    - Added [WebView.Evaluate](https://www.mbsplugins.eu/WebViewEvaluate.shtml) function to evaluate JavaScript expression and get back result for Windows.

### Example Databases

- [List/Sort Benchmark](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/List/Sort%20Benchmark.shtml#8ScriptAnchor_)
- [WebViewer/Custom WebView](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/WebViewer/Custom%20WebView.shtml#11ScriptAnchor_)
- [WebViewer/JavaScript Evaluate](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/WebViewer/JavaScript%20Evaluate.shtml#1ScriptAnchor_)
- [WebViewer/WebViewer YouTube](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/WebViewer/WebViewer%20YouTube.shtml#3ScriptAnchor_)

### Blog Entries

- [Printing a WebViewer in FileMaker](https://www.mbsplugins.de/archive/2024-02-16/Printing_a_WebViewer_in_FileMa/monkeybreadsoftware_blog_filemaker)
- [Using PerformScript in a custom WebViewer in FileMaker](https://www.mbsplugins.de/archive/2024-01-10/Using_PerformScript_in_a_custo/monkeybreadsoftware_blog_filemaker)
- [Control interactive containers in FileMaker](https://www.mbsplugins.de/archive/2023-12-19/Control_interactive_containers/monkeybreadsoftware_blog_filemaker)
- [Run JavaScript synchronously in a WebViewer](https://www.mbsplugins.de/archive/2022-12-19/Run_JavaScript_synchronously_i/monkeybreadsoftware_blog_filemaker)
- [Working with Webkit Message Handlers in FileMaker](https://www.mbsplugins.de/archive/2020-06-05/Working_with_Webkit_Message_Ha/monkeybreadsoftware_blog_filemaker)
- [Check out options for FileMaker JavaScript Integration](https://www.mbsplugins.de/archive/2020-05-21/Check_out_options_for_FileMake/monkeybreadsoftware_blog_filemaker)
- [New Examples 2020](https://www.mbsplugins.de/archive/2020-03-23/New_Examples_2020/monkeybreadsoftware_blog_filemaker)
- [FileMaker and JavaScript - the perfect combination](https://www.mbsplugins.de/archive/2020-02-03/FileMaker_and_JavaScript_-_the/monkeybreadsoftware_blog_filemaker)
- [New in the MBS FileMaker Plugin 10.0](https://www.mbsplugins.de/archive/2020-01-23/New_in_the_MBS_FileMaker_Plugi/monkeybreadsoftware_blog_filemaker)
- [Automate web viewer in FileMaker](https://www.mbsplugins.de/archive/2020-01-20/Automate_web_viewer_in_FileMak/monkeybreadsoftware_blog_filemaker)

### FileMaker Magazin

- [Ausgabe 3/2024, Seite 32 bis 33](https://filemaker-magazin.de/neuigkeit/4263-Appetithappen-FMM_202403)

This function checks for a license.

Created 4th January 2020 , last changed 5th December 2023

  
[WebView.EstimatedProgress](WebViewEstimatedProgress.md) - [WebView.ExecCommand](WebViewExecCommand.md)

[HTML Version](WebViewEvaluate.shtml)