How do I trigger a javascript event with HTMLViewer?

Hello,
I’m using an HTMLView component to view an OpenStreetMap map in my application. How can I retrieve the GPS coordinates displayed when I click on the map? I’ve been trying for hours without success. In particular, the JavaScriptRequest event is never activated.
Thanks for your help,
Dan

There is currently an issue with using executeInXojo on Linux, if that’s your target. In the future, it’d be helpful if you supply your system information, Xojo version, and an example project so we can help.

The first place to start would be the HTMLViewer JavaScript Execution example project that you can open from the Examples section of the IDE’s New Project window. If all of those functions work, then it’s something in your project and you can analyze the code in that project to determine the root of your own error.

If you’re not targeting Linux, then it’s likely you have an error in your JavaScript code. Could be a malformed event listener, missing semicolon, and so on.

2 Likes

What action are you taking on the Xojo side to trigger the javascript event on the HTMLViewer side? Can we see the code?

For Linux, although executeInXojo() doesn’t work there, you should still be able to trigger the event on the HTMLViewer side from Xojo, just that Linux will not be able to confirm that back to the Xojo side (or send any data). But using the TitleChanged event still works, at least for short results, so I’m using that.

I’ll check what I actually do in more detail and post again.

Javascript on the HTMLViewer side.:

function requestImage ()

     {
     titleCount++;
     document.title = "composerEvent:" + titleCount + ":GetURL";
     }

Note the titleCount business. I discovered a long time ago that if you set the title to what it already is, that request gets ignored. Anyway with the above I can parse the new title in Xojo and take the required action.

From the Xojo side I do similar to:

html_body = MainWindow.ExecuteJavaScriptSync ("getHTML ();")

where getHTML is a javascript method which returns the HTML as a string, from the HTMLViewer side. The results of running that javascript are then to be found on the Xojo side in html_body.

Basically I’m using TitleChanged as a communications channel between the two sides. Once executeInXojo works on Linux, I’ll be able to remove that and just use the new Xojo methods. When these arrived, it also meant I could retire the WebSocket Server I’d implemented in the app for all such communications between Xojo and the innards of the HTMLViewer. It worked well enough but I always felt it to be a cumbersome way to do the job.

Thanks. I’m under macOS not Linux. I attach a small example project
interactive HTML map.xojo_binary_project.zip (7.5 KB)

There is no window.Xojo object, thus no window.Xojo.Call method. Not sure where you got that from. You’re also passing the parameters as an array, which is unsupported. Change line 36 of Button1.Pressed to the following:

"        if (executeInXojo) { executeInXojo('coords', lat, lng); }" + EndOfLine + _
1 Like