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: 7.2   7.3   7.4   7.5   8.0   8.1   8.2   8.3   8.4   8.5    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 )   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

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!

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 Veiwer \"" & 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.

See also

Example Databases

Created 18th August 2014, last changed 17th September 2018


WebView.RenderPDF   -   WebView.RunJavaScriptReturnTitle

Feedback: Report problem or ask question.




Links
MBS Xojo Chart Plugins