RegisterEvent

Description

Registers the PowerBuilder user defined event so they can be triggered in JavaScript.

Applies to

WebBrowser controls

Syntax

controlname.RegisterEvent (string eventname) 

Argument

Description

controlname

The name of the WebBrowser control.

eventname

The name of the user-defined event to be registered.


Return value

Integer.

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

  • 1 -- Success

  • -1 -- Failed to execute the line.

  • -2 -- Failed to initialize the control and obtain the WebBrowser object. The WebView2 is invalid.

  • -4 -- The target path does not exist.

  • -5 -- The URI is invalid.

  • -6 -- The specified download task does not exist.

  • -8 -- The setting item does not exist.

  • -9 -- The path is invalid or does not exist.

  • -11 -- Unknown error.

  • -12 -- Invalid event name.

  • -14 -- Invalid argument.

  • -15 -- Failed to create screenshot file.

  • -16 -- There is a print job in progress, which cannot be closed.

  • -17 -- Could not find the corresponding item.

Usage

The initialization of the WebBrowser control is run as a separate process from the PowerBuilder application. Therefore, if the WebBrowser control is being initialized while you register the event in PowerBuilder, the registration will fail. To avoid such issue, it is recommended that you trigger the NavigateStart event first. If NavigateStart can be triggered successfully, it indicates that WebBrowser is completely initialized. For example, you can add the following code to the NavigateStart event:

IF ib_RegisterEvent = FALSE THEN                               
   Int li_rc 
   li_rc = wb_1.RegisterEvent ( "ue_getstring" ) 
   IF li_rc =1 THEN
      ib_RegisterEvent =TRUE
   END IF
END IF

Example 1

The following example defines a PowerBuilder user event which is then triggered in JavaScript in wb_1, and the result of the JavaScript which is returned in a JSON string is parsed by the JSONParser object.

Note that this user event should not call any other WebBrowser function, otherwise the application may get stuck.

//define a user event: ue_getstring in wb_1
//event type string ue_getstring(string as_arg);
string ls_String
ls_String = "This is PB Event!" + "~r~nFrom JavaScript:" + as_arg
Return ls_String
//end event
Integer li_Return
String ls_JS, ls_Result, ls_Error
JsonParser lnv_JsonParser
Long ll_RootObject
String ls_Type, ls_Value

li_Return = wb_1.RegisterEvent("ue_getstring")
If li_Return = 1 Then
        ls_JS = "function event1() { return window.webBrowser.ue_getstring('Hi,PB!');} event1();"
        li_Return = wb_1.EvaluateJavascriptSync(ls_JS, ls_Result, ls_Error)
        If li_Return = 1 Then
               lnv_JsonParser = Create JsonParser
               lnv_JsonParser.LoadString(ls_Result)
               ll_RootObject = lnv_JsonParser.GetRootItem()
               ls_Value = lnv_JsonParser.GetItemString( ll_RootObject, "value" )
        End If
End If
//{"type":"string","value":"This is PB Event!\r\nFrom JavaScript:Hi,PB!"}
MessageBox( "Tips", ls_Result )

//This is PB Event!
//From JavaScript:Hi,PB!
MessageBox( "Tips", ls_Value )

Example 2

The following example registers a PowerBuilder user event in the WebBrowser NavigationProgressIndex event, and then triggers the user event in JavaScript in an HTML file.

Step 1: Defines the user event: ue_getstring in the WebBrowser control.

//event type string ue_getstring(string as_arg);
string ls_String
ls_String = "This is PB Event!" + "~r~nFrom JavaScript:" + as_arg
sle_1.text = ls_String
Return ls_String
//end event

Step 2: Registers the user event: ue_getstring in the WebBrowser NavigationProgressIndex event.

Note that the NavigationProgressIndex event will be triggered multiple times when the HTML page is being loaded.

//Defines an instance variable ib_flag to determine whether the user event has been registered
boolean ib_flag = true
//event navigationprogressindex;
Integer li_Return
If progressindex =100 Then 
 If ib_flag Then 
  li_Return = wb_1.RegisterEvent("ue_getstring")
  If li_Return = 1 Then
   ib_flag = False
  End If
 End If
End If
//end event

Step 3: Triggers the user event in JavaScript in the HTML page: page.html.

<!DOCTYPE html>
<html>
<header>
<meta charset="UTF-8">
</header>
<body>

<button type="button" onclick="window.webBrowser.ue_getstring('Hi,PB!')">
Call PB event
</button>

<button type="button" onclick="CallPBEvent()">
Call PB event with return
</button>

<script>
function CallPBEvent(){
                var ret = window.webBrowser.ue_getstring('Hi,PB!');
                alert(ret);
}

</script>
</body>
</html>

Step 4: Loads the HTML page in the WebBrowser control.

wb_1.Navigate("file:///page.html")

Example 3

The following example defines a user event which is then triggered in JavaScript in wb_1:

//define a user event: ue_getstring in wb_1
//event type string ue_getstring(string as_arg);
string ls_String
ls_String = "This is PB Event!" + "~r~nFrom JavaScript:" + as_arg
Return ls_String
//end event
Integer li_Return
String ls_JS, ls_Result, ls_Error
JsonParser lnv_JsonParser
Long ll_RootObject
String ls_Type, ls_Value

li_Return = wb_1.RegisterEvent("ue_getstring")
If li_Return = 1 Then
        ls_JS = "function event1() { return window.webBrowser.asyncfun.prototype.ue_getstring('Hi,PB!');} event1();"
        li_Return = wb_1. EvaluateJavascriptAsync (ls_JS)
End If

See also

CancelDownload

EvaluateJavascriptAsync

EvaluateJavascriptSync

GetSource

GoBack

GoForward

Navigate

PrintAsPDF

PauseDownload

ResumeDownload

Refresh

StopNavigation

UnregisterEvent

Zoom