1. Miguel Leeuwe
  2. PowerBuilder
  3. Friday, 16 April 2021 15:18 PM UTC

Hi,

I'm trying to trigger a powerbuilder user event from the new webbrowser control.

The help on registerEvent() has this nice example, but it doesn't tell you how to trigger the event from html/javascript. Has anyone done this?

TIA.

//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 )
Accepted Answer
Arthur Hefti Accepted Answer Pending Moderation
  1. Friday, 16 April 2021 16:19 PM UTC
  2. PowerBuilder
  3. # Permalink

Hi Miguel

with the sample JS defines a JavaScript function and calls it.

below a simple html that you can load into the web browser control and then click on the button.

<html>
<body>
<button onclick="alert( event1() );">GetString</button>
</body>
</html>

Before this you need to load the page and define the event in PB (quite dirty code)

string ls_JS, ls_Error, ls_Result
wb_1.Navigate( "htmlfile.html" )

wb_1.RegisterEvent( "ue_getstring" )

ls_JS = "function event1() { return window.webBrowser.ue_getstring('Hi,PB!');}"
wb_1.EvaluateJavascriptSync(ls_JS, ls_Result, ls_Error)

Of course you can define the script in the html as well, so it would be navigate and register event only and JS would be e"vent1();" only.

 

<html>
<body>
<script>
function event1() {
return window.webBrowser.ue_getstring('Hi,PB!')
}
</script>

<button onclick="alert( event1() );">GetString</button>
</body>
</html>

Regards
Arthur

Comment
  1. Miguel Leeuwe
  2. Friday, 16 April 2021 18:57 PM UTC
Thank you very much Arthur, gonna try this out!
  1. Helpful
  1. Miguel Leeuwe
  2. Saturday, 17 April 2021 06:46 AM UTC
That worked! Thank you so much Arthur. This is really a powerful feature. I'd never thought I'd become interested in learning Javascript.
  1. Helpful
There are no comments made yet.
Miguel Leeuwe Accepted Answer Pending Moderation
  1. Saturday, 17 April 2021 08:02 AM UTC
  2. PowerBuilder
  3. # 1

(EDITED).

Please ignore my previous email (see below).
I didn't realize there was an extra call to "event1();" at the end of the line
ls_JS = "function event1() { return window.webBrowser.ue_getstring('DO NOT DO ANYTHING');} event1();"

This is an email I sent to Product@appeon.com:
-----

Hi,

These 2 lines come from the pb help on the RegisterEvent() function:
ls_JS = "function event1() { return window.webBrowser.ue_getstring('Hi,PB!');} event1();"
li_Return = wb_1.EvaluateJavascriptSync(ls_JS, ls_Result, ls_Error)

I really like the possibility to be able to define functions "on the fly". Securitywise, this is a great, as it allows you to define functions in powerbuilder and not have them in some html or js files where a user could manipulate them.

But ...
When running EvaluateJavascriptSync(), the function is also executed.
It would be nice to have the possibility to add a function without running it.
Maybe an extra parameter?

As an example: I'm trying to get the TinyMCE html editor working.
There's a "save_onsavecallback" function, which is run when you have edited some text and the Save button has become enabled.
From this callback function, I want to call a powerbuilder event that takes care of the saving process.

Right now, the only clean way to do that, is by pre-defining the function in html and not use EvaluateJavascriptSync() to create the function.

I could maybe do something like:
ls_JS = "function event1() { return window.webBrowser.ue_getstring('DO NOT DO ANYTHING');} event1();"
And then in ue_getstring() do nothing if the argument is "DO NOT DO ANYTHING", but that's not the best solution.

Kind regards,
Miguel Leeuwe

Comment
  1. Arthur Hefti
  2. Sunday, 18 April 2021 06:57 AM UTC
Hi Miguel

have a look at ls_JS, this contains the definition of the function and the call to it:

Definition:

function event1() {

return window.webBrowser.ue_getstring('DO NOT DO ANYTHING');

}



Call:

event1();



When you leave the part event1(), you define the function without calling it.

Regards

Arthur
  1. Helpful
  1. Miguel Leeuwe
  2. Sunday, 18 April 2021 19:31 PM UTC
Hi Arthur,

Ah now I see what you mean, I don't even know how that call got there :)

(copy paste mess)

thanks!
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.