EvaluateJavascriptSync

Description

Executes JavaScript synchronously.

Note

EvaluateJavascriptSync is a synchronous function and it cannot be called in the WebBrowser event, otherwise the application will get stuck. The WebBrowser event can only call the asynchronous function EvaluateJavascriptAsync, but not any other synchronous function including EvaluateJavascriptSync.

Applies to

WebBrowser control

Syntax

controlname.EvaluateJavascriptSync (string script{, ref string result{, ref string error}})

Argument

Description

controlname

The name of the WebBrowser control.

script

The JavaScript to be evaluated. The JavaScript cannot have statements that show dialog boxes. If a dialog box must be displayed in JavaScript, the script must be executed asynchronously.

If there are multiple JavaScript statements, only the last JavaScript statement will have its result returned.

result

The result of script execution. The execution result is represented in a JSON string. The supported JavaScript data types are bool, int, double, string, date, array. When the above types are mapped to the PowerBuilder data types, they are boolean, integer, double, string, datetime, array.

For example,

{"type":"double","value":1585620350123}; //returns a double-type value

{"type":"string","value":"12d6_1585674123456_74563"}; //returns a string-type value

error

The error information if an error occurs during execution. The error information is represented in a JSON string. For example,

{"type":"error","value":"Uncaught TypeError: Cannot read property 'style' "}


Return value

Integer.

Returns values as follows. If any argument's value is null, the method returns null.

1 -- Success.

-1 -- General error.

-2 -- WebView2 control is invalid.

-14 -- Invalid argument.

Examples

The following example executes a JavaScript and gets the current time (double type):

String ls_JS, ls_Result, ls_Error
Integer li_Return

ls_JS = "(new Date()).getTime();"
li_Return = wb_1.EvaluateJavascriptSync(ls_JS, ls_Result)

The following example executes a JavaScript and returns an array:

String ls_JS, ls_Result, ls_Error
Integer li_Return

ls_JS = "new Array(123, new Date(), 'Appeon', 2 > 1)"
li_Return = wb_1.EvaluateJavascriptSync(ls_JS, ls_Result, ls_Error)

The following example executes a JavaScript and parses the result (a JSON string) via the JSONParser object:

String ls_JS, ls_Result, ls_Error, ls_Type
Integer li_Return
DateTime ldt_DateTime
JsonParser lnv_JsonParser
Long ll_RootObject

lnv_JsonParser = Create JsonParser

ls_JS = "function getNow(){return (new Date());} getNow();"
li_Return = wb_1.EvaluateJavascriptSync(ls_JS, ls_Result, ls_Error)

If li_Return = 1 Then
 lnv_JsonParser.LoadString(ls_Result)
 ll_RootObject = lnv_JsonParser.GetRootItem()
 ls_Type = lnv_JsonParser.GetItemString(ll_RootObject, "type")
 ldt_DateTime = lnv_JsonParser.GetItemDateTime(ll_RootObject, "value")
End If

If IsValid(lnv_JsonParser) Then Destroy (lnv_JsonParser)

See also

CancelDownload

EvaluateJavascriptAsync

GetSource

GoBack

GoForward

Navigate

PrintAsPDF

PauseDownload

RegisterEvent

ResumeDownload

Refresh

StopNavigation

UnregisterEvent

Zoom