Javascript addEventListener

I have a windows program with HTMLViewer webkit as the browser. I’m trying to call a method if a button is clicked in the browser. If possible would I just have a timer listen for the javascript button click? I see theres an addEventListener for javascript but in the example it just changes the innerhtml of a button on a webpage. I’d like it to call a method in Xojo. Is this possible? Thanks.

document.getElementById(“myBtn”).addEventListener(“click”, function() {
document.getElementById(“demo”).innerHTML = “Hello World”;

Hi John,
You can do this by using the executeInXojo JavaScript function. Something like this:

document.getElementById("myBtn").addEventListener("click", function() {

Then implement the JavaScriptRequest event of the HTMLViewer.

You should take a look at the HTMLViewer-Exec example project included with the IDE at /Example Projects/Desktop/Controls

1 Like

Thanks Anthony, I’ll give it a try.

1 Like

Not sure if my syntax is wrong here or I’m just way off but this is what I have and it is not working. There is a button with the ID of nav-cliear-all. When it is clicked in HTMLViewer I’d like a msgbox to pop up saying, “found”.

HTMLViewer1.ExecuteJavascript(“document.getElementById(‘nav-clear-all’).addEventListener(”“click”“, function() {executeInXojo(”“msgbox(”“found”“)”“);});”)

That is wrong. executeInXojo doesn’t execute a string of Xojo code, it raises the JavaScriptRequest event as I outlined above. Based on the parameters of that HTMLViewer event, you can the perform whatever operations you need to.

1 Like

Got it. I had never used the JavaScriptRequest event before. It’s working now but it is calling the JavaScriptRequest 4 times when the buttin is clicked. I have a timer that runs every second and checks for statstart = “yes”, if it is yes and the button is clicked it raises the javascriptrequest and the msgbox(“found”) is displayed. After the msgbox I change statstart back to “no” so it should not fire again but it does 3 more times. Is this due to multiple frames on the page?

if statstart = "yes" then
  HTMLViewer1.ExecuteJavascript("document.getElementById('bet-nav-clear-all').addEventListener(""click"", function() {executeInXojo(""buttonClick"");});")
end if

I figured out a way to escape from the 3 other button clicks. Thanks for the help.