RegisterEvent

Description

Registers the PowerBuilder user event so they can be triggered in JavaScript. For how to define and trigger the PowerBuilder user event, refer to Triggering PowerBuilder events in JavaScript.

Note

The user event should not call any other WebBrowser function, except the asynchronous function EvaluateJavascriptAsync. Calling the synchronous function (such as EvaluateJavascriptSync) will cause the application to get stuck.

Applies to

WebBrowser control

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 1 if the function succeeds and a negative value if an error occurs. If any argument's value is null, the method returns null.

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

Note that the user event should not call any other WebBrowser function (except EvaluateJavascriptAsync), otherwise the application may get stuck.

For example, in the following user event, you cannot call the WebBrowser function such as Navigate, Refresh, or EvaluateJavascriptSync.

//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
sle_1.text = ls_String
//wb_1.Navigate(ls_url)  //Cannot call the WebBrowser Navigate function in this user event, otherwise exception will occur.
//wb_1.Refresh()  //Cannot call the WebBrowser Refresh function in this user event, otherwise exception will occur.
//wb_1.EvaluateJavascriptSync(ls_js)  //Cannot call the WebBrowser EvaluateJavascriptSync function in this user event, otherwise exception will occur.
Return ls_String
//end event

For another example, in the following user event, you can only call the WebBrowser EvaluateJavascriptAsync function.

//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
wb_1.EvaluateJavascriptAsync(ls_JS) //You can only call the EvaluateJavascriptAsync function in this user event.
Return ls_String
//end event

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.

//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. It is recommended to execute the next script after the NavigationProgressIndex event receives the value of 100 (which means the page is 100% 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