# HTMLViewer: Xojo + JavaScript Communication

**URL:** <https://forum.xojo.com/t/htmlviewer-xojo-javascript-communication/56767>\
**Category:** Windows\
**Created:** [September 5, 2020, 11:53pm UTC](https://forum.xojo.com/t/htmlviewer-xojo-javascript-communication/56767 "2020-09-05T23:53:10Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anthony\_G\_Cyphers](https://forum.xojo.com/user_avatar/forum.xojo.com/anthony_g_cyphers/32/16862_2.png) [@Anthony\_G\_Cyphers](https://forum.xojo.com/u/Anthony_G_Cyphers)\
**Post date:** [September 5, 2020, 11:53pm UTC](https://forum.xojo.com/t/htmlviewer-xojo-javascript-communication/56767/1 "2020-09-05T23:53:10Z")

</div>

To further elaborate on what @DerkJ said here:

> [@Desktop HTML viewer fail with 2020 R1](https://forum.xojo.com/t/desktop-html-viewer-fail-with-2020-r1/56379/31):
>
> The new features of the htmlviewer are worth alot since they open a full set of usages to xojo developers. You can even communicate to and from javascript to Xojo now.

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`

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [September 6, 2020, 2:07am UTC](https://forum.xojo.com/t/htmlviewer-xojo-javascript-communication/56767/2 "2020-09-06T02:07:14Z")

</div>

> [@Desktop HTML viewer fail with 2020 R1](https://forum.xojo.com/t/desktop-html-viewer-fail-with-2020-r1/56379/31):
>
> You can even communicate to and from javascript to Xojo now.

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.

---

<div class="post-metadata">

**Author:** ![DerkJ](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@DerkJ](https://forum.xojo.com/u/DerkJ)\
**Post date:** [September 6, 2020, 6:22am UTC](https://forum.xojo.com/t/htmlviewer-xojo-javascript-communication/56767/3 "2020-09-06T06:22:03Z")

</div>

> [@Desktop HTML viewer fail with 2020 R1](https://forum.xojo.com/t/desktop-html-viewer-fail-with-2020-r1/56379/35):
>
> 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.

> [@Anthony\_Cyphers](#):
>
> 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`

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

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [September 6, 2020, 11:40am UTC](https://forum.xojo.com/t/htmlviewer-xojo-javascript-communication/56767/4 "2020-09-06T11:40:01Z")

</div>

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:

```auto
<!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>

```

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [September 6, 2020, 11:42am UTC](https://forum.xojo.com/t/htmlviewer-xojo-javascript-communication/56767/5 "2020-09-06T11:42:43Z")

</div>

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

HTMLViewer1.ExecuteJavaScript(“myFunction();”)

---

<div class="post-metadata">

**Author:** ![Anthony\_G\_Cyphers](https://forum.xojo.com/user_avatar/forum.xojo.com/anthony_g_cyphers/32/16862_2.png) [@Anthony\_G\_Cyphers](https://forum.xojo.com/u/Anthony_G_Cyphers)\
**Post date:** [September 6, 2020, 11:44am UTC](https://forum.xojo.com/t/htmlviewer-xojo-javascript-communication/56767/6 "2020-09-06T11:44:00Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [September 6, 2020, 11:46am UTC](https://forum.xojo.com/t/htmlviewer-xojo-javascript-communication/56767/7 "2020-09-06T11:46:23Z")

</div>

> [@Anthony\_Cyphers](#):
>
> it’s actually `executeInXojo`

Adjusted. Nothing changed.

---

<div class="post-metadata">

**Author:** ![Anthony\_G\_Cyphers](https://forum.xojo.com/user_avatar/forum.xojo.com/anthony_g_cyphers/32/16862_2.png) [@Anthony\_G\_Cyphers](https://forum.xojo.com/u/Anthony_G_Cyphers)\
**Post date:** [September 6, 2020, 11:46am UTC](https://forum.xojo.com/t/htmlviewer-xojo-javascript-communication/56767/8 "2020-09-06T11:46:45Z")

</div>

> [@Rick\_Araujo](#):
>
> I also can’t call myFunction() from Xojo, it causes an OLEException (Windows):

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.

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [September 6, 2020, 11:48am UTC](https://forum.xojo.com/t/htmlviewer-xojo-javascript-communication/56767/9 "2020-09-06T11:48:21Z")

</div>

> [@Anthony\_Cyphers](#):
>
> switch over to WebKit

THAT did the trick.

---

<div class="post-metadata">

**Author:** ![Anthony\_G\_Cyphers](https://forum.xojo.com/user_avatar/forum.xojo.com/anthony_g_cyphers/32/16862_2.png) [@Anthony\_G\_Cyphers](https://forum.xojo.com/u/Anthony_G_Cyphers)\
**Post date:** [September 6, 2020, 11:48am UTC](https://forum.xojo.com/t/htmlviewer-xojo-javascript-communication/56767/10 "2020-09-06T11:48:37Z")

</div>

Happy to hear it.

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [September 6, 2020, 11:49am UTC](https://forum.xojo.com/t/htmlviewer-xojo-javascript-communication/56767/11 "2020-09-06T11:49:29Z")

</div>

Well, we have material for the docs here.

---

<div class="post-metadata">

**Author:** ![Anthony\_G\_Cyphers](https://forum.xojo.com/user_avatar/forum.xojo.com/anthony_g_cyphers/32/16862_2.png) [@Anthony\_G\_Cyphers](https://forum.xojo.com/u/Anthony_G_Cyphers)\
**Post date:** [September 6, 2020, 11:50am UTC](https://forum.xojo.com/t/htmlviewer-xojo-javascript-communication/56767/12 "2020-09-06T11:50:07Z")

</div>

Well, let’s tag @Paul_Lefebvre!

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [September 6, 2020, 11:56am UTC](https://forum.xojo.com/t/htmlviewer-xojo-javascript-communication/56767/13 "2020-09-06T11:56:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Anthony\_G\_Cyphers](https://forum.xojo.com/user_avatar/forum.xojo.com/anthony_g_cyphers/32/16862_2.png) [@Anthony\_G\_Cyphers](https://forum.xojo.com/u/Anthony_G_Cyphers)\
**Post date:** [September 6, 2020, 11:57am UTC](https://forum.xojo.com/t/htmlviewer-xojo-javascript-communication/56767/14 "2020-09-06T11:57:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [September 6, 2020, 11:59am UTC](https://forum.xojo.com/t/htmlviewer-xojo-javascript-communication/56767/15 "2020-09-06T11:59:43Z")

</div>

> [@Anthony\_Cyphers](#):
>
> they may have told me

That’s a matter for the docs.

---

<div class="post-metadata">

**Author:** ![Anthony\_G\_Cyphers](https://forum.xojo.com/user_avatar/forum.xojo.com/anthony_g_cyphers/32/16862_2.png) [@Anthony\_G\_Cyphers](https://forum.xojo.com/u/Anthony_G_Cyphers)\
**Post date:** [September 6, 2020, 12:00pm UTC](https://forum.xojo.com/t/htmlviewer-xojo-javascript-communication/56767/16 "2020-09-06T12:00:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [September 6, 2020, 12:03pm UTC](https://forum.xojo.com/t/htmlviewer-xojo-javascript-communication/56767/17 "2020-09-06T12:03:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TimStreater](https://forum.xojo.com/user_avatar/forum.xojo.com/timstreater/32/586_2.png) [@TimStreater](https://forum.xojo.com/u/TimStreater)\
**Post date:** [September 6, 2020, 7:03pm UTC](https://forum.xojo.com/t/htmlviewer-xojo-javascript-communication/56767/18 "2020-09-06T19:03:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TimStreater](https://forum.xojo.com/user_avatar/forum.xojo.com/timstreater/32/586_2.png) [@TimStreater](https://forum.xojo.com/u/TimStreater)\
**Post date:** [September 6, 2020, 7:08pm UTC](https://forum.xojo.com/t/htmlviewer-xojo-javascript-communication/56767/19 "2020-09-06T19:08:17Z")

</div>

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](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).

---

<div class="post-metadata">

**Author:** ![TimStreater](https://forum.xojo.com/user_avatar/forum.xojo.com/timstreater/32/586_2.png) [@TimStreater](https://forum.xojo.com/u/TimStreater)\
**Post date:** [September 6, 2020, 7:37pm UTC](https://forum.xojo.com/t/htmlviewer-xojo-javascript-communication/56767/20 "2020-09-06T19:37:04Z")

</div>

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

[Next page](https://forum.xojo.com/t/htmlviewer-xojo-javascript-communication/56767.md?page=2)
