Contents - Index - Top


RunAnonymousFunction

Interface AntViewDocument

 

Type: Method

Parameters: Integer Id Variant Params String Script 

Returns: Integer Error

 

 

Gives you the ability to run an anonymous javascript function via the Script variable. 

As all functionality via javascript is run in an asynchronous way, you can not get any results from the javascript function directly.

You can proces the return values in OnRunAnonymousFunction event that is triggered after the function completes.

By passing in an function Id you can track which function completed in OnRunAnonymousFunction event.

You can pass your parameters directly to the anonymous function by using a  variant array for Params.

 

You need to connect your document object to the current WebView control via CurrentBrowser

Returns 0 if the script was passed on to the WebView control.

Returns 1 if the document object was not connected or 2 if your script variable is empty.

 

Example in Visual Basic 6:

  Private Sub QueryRatings()

    Dim nValue As Variant

    Dim sCurFrom As String

    Dim sCurTo As String

    Dim jScript As String

    Dim varParams(3) As Variant

    

    jScript = "(amount,curFrom,curTo) => {" & vbCrLf

    jScript = jScript & "  displayGraph(curFrom,curTo);" & vbCrLf

    jScript = jScript & "  let obj = document.getElementById('currencyRate');" & vbCrLf

    jScript = jScript & "  if (obj !== undefined) {" & vbCrLf

    jScript = jScript & "    obj.innerText = '-1'" & vbCrLf  ' -1 is initialized..

    jScript = jScript & "  }" & vbCrLf

    jScript = jScript & "  queryRates(amount,curFrom,curTo);" & vbCrLf

    jScript = jScript & "}" & vbCrLf

    nValue = CDec(AmountTextBox.Text)

    sCurFrom = FromCombo.Text

    sCurTo = ToCombo.Text

    varParams(0) = nValue

    varParams(1) = sCurFrom

    varParams(2) = sCurTo

    

    Document.RunAnonymousFunction 2, varParams, jScript

    '

    ' We use a timer to poll for the results

    Timer.Enabled = True

  End Sub

 

 

 

The anonymous javascript function above is using so called arrow function notation.

So there is no javascript function name and the 3 parameters passed (amount,curFrom,curTo) are put within the parenthesis.

These parameters line up with the parameters passed from calling RunAnonymousFunction via the Params variant array.

 

There's also a synchronous alternative RunAnonymousFunctionSync method that wraps this method and the event into one call.

 

In case your programming language does not support variant arrays as used by the Params variable, then there is an alternative way to add data to the javascript parameters in a function.

Let's take the example above and add the same data, but now without the Params array.

 

Instead of this:

   jScript = "(amount,curFrom,curTo) => {" & vbCrLf

Use this:

   nValue = CDec(AmountTextBox.Text)

   If nValue <> 0 Then

     sCurFrom = FromCombo.Text

     sCurTo = ToCombo.Text

     jScript = ("(amount =" & Cstr(nValue) & " ,curFrom = '" & sCurFrom & "', curTo = '" & sCurTo & ") => {") & vbCrLf

    ... more code here

In other words, we are assigning the data directly in the javascript function declaration

You can then pass a value 0 instead of the Params variable.

    Document.RunAnonymousFunction 2, 0, jScript

 

 

This then adds the value by the means of a javascript parameter default. 

Since we no longer pass the variables, the default value becomes the data.

 

If you need to use the javascript parameter default workaround, then one thing to look out for is that javascript expects a dot notation when you pass a decimal value.

So amount=9.00 is correct, but amount=9,00 will not work.

 

See also:

OnRunAnonymousFunction, AnonymousFunctionReturnParams, RunAnonymousFunctionSync, Debug anonymous javascript functions

 

Example:

VB6 - Exchange rates example

 


AntView - The MS Edge WebView2 ActiveX control Date last changed: 04/16/2025