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 DataFlex:
Define CFun_QueryCurrencyRate for 6
Define CFun_RateReceived for 7
Procedure QueryRatings
String sScript
Integer iErr
Number nAmount
String sCurFrom
String sCurTo
Variant[] Params
Move "" To sScript
Append sScript "(amount,curFrom,curTo) => {" CS_CRLF
Append sScript " displayGraph(curFrom,curTo);" CS_CRLF
Append sScript " let obj = document.getElementById('currencyRate');" CS_CRLF
Append sScript " if (obj !== undefined) {" CS_CRLF
Append sScript " obj.innerText = '-1'" CS_CRLF // -1 is initialized..
Append sScript " }"
Append sScript " queryRates(amount,curFrom,curTo);" CS_CRLF
Append sScript "}" CS_CRLF
Send ConnectBrowser to oDocument
Get Value of oAmountForm to nAmount
Get Value of oFromCombo to sCurFrom
Get Value of oToCombo to sCurTo
Move nAmount To Params[0]
Move sCurFrom To Params[1]
Move sCurTo To Params[2]
Get ComRunAnonymousFunction of oDocument CFun_QueryCurrencyRate Params sScript to iErr
End_Procedure
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:
Append sScript "(amount,curFrom,curTo) => {" CS_CRLF
Use this:
Get Value of oAmountForm to nAmount
Get Value of oFromCombo to sCurFrom
Get Value of oToCombo to sCurTo
Append sScript '(amount = ' String(nAmount) ' ,curFrom ="'+sCurFrom+'",curTo ="'+sCurTo+'") => {' CS_CRLF
... more code here
You can then pass a value 0 instead of the Params variable.
Get ComRunAnonymousFunction of oDocument CFun_QueryCurrencyRate 0 sScript to iErr
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.
AntView - The MS Edge WebView2 ActiveX control Date last changed: 09/25/2024