HTMLViewer: Xojo + JavaScript Communication

To further elaborate on what @DerkJ said here:

You can find an example for the new functionality of HTMLViewer that is included in 2020R1 in the Examples folder at
Example Projects/Desktop/Controls/HTMLViewer-Exec.xojo_binary_project

1 Like

I don’t see a way of sending content to Xojo when clicking an HTML button as it looks the ExecuteInXojo() method is not available from the HtmlViewer document onClick event. It means no JS originated call to Xojo, only Xojo originated to JS.

Docs are wrong see the example.
It’s “sendToXojo()” and “sendToXojoAsync()”

1 Like

Those examples does not do what I said. They are Xojo side initiated only.

Try to fire a JavaScriptRequest() event clicking a button from an HTML like:

<!DOCTYPE html>
<html>
<body>

<h1>Test</h1>

<p>Click the button trying intercomm with Xojo.</p>

<button onclick="myFunction()">Try to fire JavaScriptRequest() at Xojo</button>

<p id="demo"></p>

<script>
function myFunction() {
  document.getElementById("demo").innerHTML = 'Firing ExecuteInXojo()';
  ExecuteInXojo('FireJavaScriptRequest', 'anyParameter');
  document.getElementById("demo").innerHTML = 'ExecuteInXojo() "fired"';
}
</script>

</body>
</html>

I also can’t call myFunction() from Xojo, it causes an OLEException (Windows):

HTMLViewer1.ExecuteJavaScript(“myFunction();”)

Hi Rick!
If you check the jsArrayAsync constant on the Window, you’ll see how to properly do this. You’re calling it as ExecuteInXojo when it’s actually executeInXojo. JavaScript is case sensitive.

If you want to setup a function to call later, the easiest way is to add it to the JavaScript Window object like this:

window.myFunction = function() {
  document.getElementById("demo").innerHTML = 'Firing ExecuteInXojo()';
  executeInXojo('FireJavaScriptRequest', 'anyParameter');
  document.getElementById("demo").innerHTML = 'ExecuteInXojo() "fired"';
}
1 Like

Adjusted. Nothing changed.

If you’re using the Native renderer, it’s hard to tell what you might be running in to. You should make sure that the Native renderer is using an up-to-date engine (search for HTMLViewer Native IE11 on the forums and you should find information on this) or just switch over to WebKit.

THAT did the trick.

Happy to hear it.

Well, we have material for the docs here.

Well, let’s tag @Paul_Lefebvre!

2 Likes

To guarantee the Xojo/JS intercomm, one must use the Xojo provided engine (Webkit instead of Native).

Yeah, they may have told me that when I was building that demo project and I forgot. It’s not unreasonable given the disparity between available features in the engines that the various versions of Windows actually gives access to.

That’s a matter for the docs.

We tagged Paul, let the man get a little rest! We already work the whole team to death! :wink:

Just calling attention to the fact, not activating an urgent demand. :grin:

2 Likes

Yes you can, I just tried it in macOS. I put in the HTMLViewer this html:

<!doctype html>
<html>
<head>
<script type="text/javascript">
function doButton()
     {
     executeInXojo ('somecommand', 'Have this string for grins.'); 
     }
</script>
</head>
<body>
<input type="button" value="Click me" onclick="doButton()">
</body>
</html>

I added the JavaScriptRequest event handler to the HTMLViewer, and it fired and its arguments contained the values from the javascript.

I was somewhat relieved to see this, since, as has been correctly pointed out, the Xojo example is all driven from the Xojo side. There’s no unsolicited action from the HTMLViewer side. If that turned out to be impossible, I couldn’t do my plan to retire my Websocket Server from my app.

1 Like

What about IE11? Does that help. You have to not only have IE11 installed on your machine, you also have to force it to render in IE11 mode. See here:

https://documentation.xojo.com/api/deprecated/htmlviewer.html

and be sure to do all thie things listed under Windows IE version. I discovered all those things myself the hard way (which is why they are now documented on that page).

Hmmm. Just tried it myself in my Win7/10 VMs, nothing happens. Guess I’ll have to try in the debugger under 2020r1