Topics   All   Mac OS X (Only)   Windows (Only)   Linux (Only, Not)   iOS (Only, Not)  
Components   Crossplatform Mac & Win   Server   Client   Old   Guides   Examples
New in version: 8.3   8.4   8.5   9.0   9.1   9.2   9.3   9.4   9.5   10.0    Statistic  

WebView.RunJavaScript

Executes the Javascript on the specified webViewer control.

Component Version macOS Windows Server FileMaker Cloud FileMaker iOS SDK
WebView 1.3 Yes Yes No No Yes
MBS( "WebView.RunJavaScript"; WebViewerRef; Javascript { ; Async } )   More

Parameters

Parameter Description Example value
WebViewerRef Either the Web Viewer Object Name or the Web Viewer ID as returned by "WebView.FindByName" function.
Javascript The Javascript to be executed
Async Optional
Available in MBS FileMaker Plugin 10.0 or newer.
Whether to run asynchronously.
On MacOS and iOS you can choose between running sync and waiting for result (and returns result) or running asnychronously and returning just OK.

For older WebKit 1.x in FileMaker 15 or older or web viewers created with WebView.Create function, we always use synchronous mode.

Windows is always synchronously.

Default is 0 for synchronous mode.
0

Description

Executes the Javascript on the specified webViewer control.
Allows you to call any Javascript function that is in loaded in the WebViewer OR you can just run arbitrary JavaScript on an empty web page. The results of the JavaScript are returned on Macintosh only. There is another function called WebView.RunJavaScriptReturnTitle which can be used to return the value of a Javascript function on both platforms. Use "\n" in javascript strings for a newline character.

Does not work on WebDirect!

See newer WebView.Evaluate function which works better on Windows and returns results.
Check JS Functions for JavaScript engine for cross platform code to run without web viewer.

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

Examples

Set the htmlcode of a field as string:

MBS( "WebView.RunJavaScript"; "browser"; "document.getElementById('td_ProcName_v').innerHTML='hello';" )

Run JavaScript

Let(
[

// very simple javascript

javascript = "alert('OK');";

webViewerName = "myWebViewer";
windowRef = MBS("Window.FindByTitle"; Get ( WindowName ));
webViewerID = MBS("WebView.FindByName"; windowRef; webViewerName)

];
Case(
webViewerID = 0; "Web Viewer \"" & webViewerName & "\" Not Found: " &windowRef ;

MBS("WebView.RunJavaScript"; webViewerID; javaScript)

)

)

Start a video inside an interactive container on Mac:

MBS( "WebView.RunJavaScript" ; "movie"; "document.getElementsByTagName('video')[0].play();" )

Send change event to send to a field with ID $FieldID so javascript code on webpage knows we changed something:

MBS( "WebView.RunJavaScript" ; "web"; "var o = document.getElementById('" & $FieldID & "');
        if (document.createEvent) {
                var evt = document.createEvent('Events');
                evt.initEvent('change', true, true);
                o.dispatchEvent(evt); }")

Clear onBeforeLoad event handler:

MBS( "WebView.RunJavaScript" ; "HTMLEditor"; "window.onbeforeunload = null;")

Send click event:

MBS( "WebView.RunJavaScript" ; "browser"; "var o = document.forms[0].loginButton;
        if (document.createEvent)
        {
                var evt = document.createEvent('Events');
                evt.initEvent('click', true, true);
                o.dispatchEvent(evt);
          }")

Filling a complex form of a web app with help of Javascript:

# go to Vouchers
Imposta variabile [ $r ; Valore: MBS( "WebView.RunJavaScript" ; "browser"; "var o = document.getElementsByClassName('appGlobalSideNav')[0].getElementsByTagName('a')[2]; if (document.createEvent) { var evt = document.createEvent('Events'); evt.initEvent('click', tr… ]
Pausa/continua lo script [ Durata (secondi): ,3 ]
# open popup
Imposta variabile [ $r ; Valore: MBS( "WebView.RunJavaScript" ; "browser"; "document.getElementsByClassName('appMainButton')[0].click();") ]
Pausa/continua lo script [ Durata (secondi): ,3 ]
# Set Values in various fields and trigger change/click events
Imposta variabile [ $r ; Valore: MBS( "WebView.SetFormInputValue" ; "browser"; "vouchersCreateCtrl.form"; "number"; "1") ]
Imposta variabile [ $r ; Valore: MBS( "WebView.RunJavaScript" ; "browser"; "var o = document.forms[0].number; if (document.createEvent) { var evt = document.createEvent('Events'); evt.initEvent('change', true, true); o.dispatchEvent(evt); }") ]
Imposta variabile [ $r ; Valore: MBS( "WebView.SetFormSelectValue" ; "browser"; "vouchersCreateCtrl.form"; "quota"; "0") ]
Imposta variabile [ $r ; Valore: MBS( "WebView.SetFormSelectValue" ; "browser"; "vouchersCreateCtrl.form"; "expire"; "custom") ]
Imposta variabile [ $r ; Valore: MBS( "WebView.RunJavaScript" ; "browser"; "var o = document.forms[0].expire; if (document.createEvent) { var evt = document.createEvent('Events'); evt.initEvent('change', true, true); o.dispatchEvent(evt); }") ]
Pausa/continua lo script [ Durata (secondi): ,3 ]
Imposta variabile [ $r ; Valore: MBS( "WebView.SetFormInputValue" ; "browser"; "vouchersCreateCtrl.form"; "expire_number"; "2") ]
Imposta variabile [ $r ; Valore: MBS( "WebView.SetFormSelectValue" ; "browser"; "vouchersCreateCtrl.form"; "expire_unit"; "1440") ]
Imposta variabile [ $r ; Valore: MBS( "WebView.RunJavaScript" ; "browser"; "var o = document.forms[0].expire_number; if (document.createEvent) { var evt = document.createEvent('Events'); evt.initEvent('change', true, true); o.dispatchEvent(evt); }") ]
Imposta variabile [ $r ; Valore: MBS( "WebView.RunJavaScript" ; "browser"; "document.forms[0].limitDownload.checked = true;") ]
Imposta variabile [ $r ; Valore: MBS( "WebView.RunJavaScript" ; "browser"; "var o = document.forms[0].limitDownload; if (document.createEvent) { var evt = document.createEvent('Events'); evt.initEvent('click', true, true); o.dispatchEvent(evt); }") ]
Pausa/continua lo script [ Durata (secondi): ,1 ]
Imposta variabile [ $r ; Valore: MBS( "WebView.SetFormInputValue" ; "browser"; "vouchersCreateCtrl.form"; "down"; "512") ]
Imposta variabile [ $r ; Valore: MBS( "WebView.RunJavaScript" ; "browser"; "var o = document.forms[0].down; if (document.createEvent) { var evt = document.createEvent('Events'); evt.initEvent('change', true, true); o.dispatchEvent(evt); }") ]
Pausa/continua lo script [ Durata (secondi): ,1 ]
Imposta variabile [ $r ; Valore: MBS( "WebView.RunJavaScript" ; "browser"; "document.forms[0].limitUpload.checked = true;") ]
Imposta variabile [ $r ; Valore: MBS( "WebView.RunJavaScript" ; "browser"; "var o = document.forms[0].limitUpload; if (document.createEvent) { var evt = document.createEvent('Events'); evt.initEvent('click', true, true); o.dispatchEvent(evt); }") ]
Pausa/continua lo script [ Durata (secondi): ,1 ]
Imposta variabile [ $r ; Valore: MBS( "WebView.SetFormInputValue" ; "browser"; "vouchersCreateCtrl.form"; "up"; "512") ]
Imposta variabile [ $r ; Valore: MBS( "WebView.RunJavaScript" ; "browser"; "var o = document.forms[0].up; if (document.createEvent) { var evt = document.createEvent('Events'); evt.initEvent('change', true, true); o.dispatchEvent(evt); }") ]
Pausa/continua lo script [ Durata (secondi): ,1 ]
Imposta variabile [ $r ; Valore: MBS( "WebView.RunJavaScript" ; "browser"; "document.forms[0].byteQuota[0].checked = true;") ]
Imposta variabile [ $r ; Valore: MBS( "WebView.RunJavaScript" ; "browser"; "var o = document.forms[0].byteQuota[0]; if (document.createEvent) { var evt = document.createEvent('Events'); evt.initEvent('click', true, true); o.dispatchEvent(evt); }") ]
Pausa/continua lo script [ Durata (secondi): ,1 ]
Imposta variabile [ $r ; Valore: MBS( "WebView.SetFormInputValue" ; "browser"; "vouchersCreateCtrl.form"; "byteQuota"; "123") ]
Imposta variabile [ $r ; Valore: MBS( "WebView.RunJavaScript" ; "browser"; "var o = document.forms[0].byteQuota[1]; if (document.createEvent) { var evt = document.createEvent('Events'); evt.initEvent('change', true, true); o.dispatchEvent(evt); }") ]
Imposta variabile [ $r ; Valore: MBS( "WebView.SetFormInputValue" ; "browser"; "vouchersCreateCtrl.form"; 8; "CLIENTE") ]
Imposta variabile [ $r ; Valore: MBS( "WebView.RunJavaScript" ; "browser"; "var o = document.forms[0].elements[11]; if (document.createEvent) { var evt = document.createEvent('Events'); evt.initEvent('change', true, true); o.dispatchEvent(evt); }") ]
Pausa/continua lo script [ Durata (secondi): ,3 ]
# press save button
Imposta variabile [ $r ; Valore: MBS( "WebView.RunJavaScript" ; "browser"; "var o = document.forms[0].elements[13]; if (document.createEvent) { var evt = document.createEvent('Events'); evt.initEvent('click', true, true); o.dispatchEvent(evt); }") ]
Pausa/continua lo script [ Durata (secondi): ,3 ]
# find the voucher ID and save it to field.
Imposta variabile [ $r ; Valore: MBS( "WebView.RunJavaScript" ; "browser"; "document.getElementById('vouchersTable').getElementsByClassName('voucherCode')[1].innerText") ]
Imposta campo [ Browser Web::VAR txt ; $r ]

Start a video inside an interactive container on Win:

MBS( "WebView.RunJavaScript" ; "player"; "document.getElementById('Player').controls.play();" )

# see https://msdn.microsoft.com/en-us/library/windows/desktop/dd562656(v=vs.85).aspx

Redirect Window.Open to trigger a FileMaker Script:

Set Variable [ $r ; Value: MBS("WebView.RunJavaScript"; "web"; "window.open = function(url, name, features, replace) { location.href='fmp://$/MyDatabase.fmp12?script=NewWindow&param='+url; }") ]
// may not work for all platforms!

Query text of an element found by it's CSS class name:

MBS( "WebView.RunJavaScript"; WebViewerRef; "document.getElementsByClassName('topteaser_master')[0].innerText")

Query duration of video in interactive container on Mac:

MBS( "WebView.RunJavaScript" ; "web"; "document.getElementsByTagName('video')[0].duration;" )

Query current time of video in interactive container on Mac:

MBS( "WebView.RunJavaScript" ; "web"; "document.getElementsByTagName('video')[0].currentTime;" )

Login for realtytrac website:

Set Web Viewer [ Object Name: "web" ; URL: "https://www.realtytrac.com/login" ]
#
# wait for website loading
#
Loop
    Pause/Resume Script [ Duration (seconds): .1 ]
    Exit Loop If [ MBS("WebView.IsLoading"; "web") ≠ 1 ]
End Loop
Pause/Resume Script [ Duration (seconds): .1 ]
#
# fill in form values with login:
Set Variable [ $r ; Value: MBS( "WebView.SetFormInputValue" ; "web"; 0; "userName"; "xxx" ) ]
Set Variable [ $r ; Value: MBS( "WebView.SetFormInputValue" ; "web"; 0; "password"; "yyy" ) ]
#
# send change events to let the javascript framework know we changed them
#
Set Variable [ $r ; Value: MBS( "WebView.RunJavaScript" ; "web"; "var o = document.getElementById('loginFormPagePassword'); if (document.createEvent)
        {
                var evt = document.createEvent('Events');
                evt.initEvent('change', true, true);
                o.dispatchEvent(evt);
          }")]
Set Variable [ $r ; Value: MBS( "WebView.RunJavaScript" ; "web"; "var o = document.forms[0].userName.value;
if (document.createEvent)
        {
                var evt = document.createEvent('Events');
                evt.initEvent('change', true, true);
                o.dispatchEvent(evt);
          }")]
#
Pause/Resume Script [ Duration (seconds): .1 ]
#
# Send click event to submit button
#
Set Variable [ $r ; Value: MBS( "WebView.RunJavaScript" ; "web"; "var o = document.getElementById('btnLoginPageSubmit'); if (document.createEvent)
        {
                var evt = document.createEvent('Events');
                evt.initEvent('click', true, true);
                o.dispatchEvent(evt);
          }")]

Go to record, wait and than jump to position to play video from there: (Mac)

# Go to record with video
Go to Record/Request/Page [ Next ; Exit after last: Off ]
# give time to load movie
Pause/Resume Script [ Duration (seconds): ,5 ]
# jump to position
Set Variable [ $r ; Value: MBS( "WebView.RunJavaScript"; "MovieView"; "document.getElementById('fm-player').currentTime = 30;" ) ]
# play
Set Variable [ $r ; Value: MBS( "WebView.RunJavaScript"; "MovieView"; "document.getElementById('fm-player').play();" ) ]

Work with form fields via JavaScript:

// Query number of forms on website:

MBS( "WebView.RunJavaScript" ; "web"; "document.forms.length;")

Query name of form number 0:

MBS( "WebView.RunJavaScript" ; "web"; "document.forms[0].name;")

Query number of elements on first form:

MBS( "WebView.RunJavaScript" ; "web"; "document.forms[0].elements.length")

Query name of first element in first form:

MBS( "WebView.RunJavaScript" ; "web"; "document.forms[0].elements[0].name")

Query value of first element in first form:

MBS( "WebView.RunJavaScript" ; "web"; "document.forms[0].elements[0].value")

Query value of element by name in form by name:

MBS( "WebView.RunJavaScript" ; "web"; "document.forms['RegisterForm'].elements['path'].value")

You can mix index or field/form names as needed.

Call doSave with try/catch to show error messages:

MBS( "WebView.RunJavaScript"; WebViewerRef; "try { doSave(); } catch(error) { alert(error);}" )

Switch windows media player for a container to fill the field:

MBS( "WebView.RunJavaScript";
"MovieField"; // name of layout element for container
document.getElementById('Player').uiMode='none';
document.body.style.transform='';
document.getElementById('Player').stretchToFit=true; " )

See also

Example Databases

Blog Entries

Release notes

  • Version 10.0
    • Added Async parameter for WebView.RunJavaScript function.
    • Changed WebView.RunJavaScript for Mac and iOS to return values in right data types and report errors better. If array or dictionary is returned, we encode it as JSON.

Created 18th August 2014, last changed 10th January 2020


WebView.RenderPDF   -   WebView.RunJavaScriptReturnTitle

Feedback: Report problem or ask question.




Links
MBS FileMaker Plugins