Executes JavaScript within the WebHTMLViewer

In Xojo Documentation I found an interesting sample see docs.xojo.com/WebHTMLViewer.LoadHTML but is not working:

Var js As String = "<script type=""text/javascript"">document.write(""Hello World!"")</script>"
Var html As String = "<html><body>" + js + "</body></html>"
HTMLViewer1.LoadHTML(html)

I’m trying to write something into a WebHTMLViewer control but I need help:

On Button pressed:

Sub Pressed()
  Method_Write_something(HTMLViewer1.ControlID)
End Sub

Method:

Sub Method_Write_something(InnerControlID As String)
  Var code() As String
  code.AddRow "function Write_something(parameter) {"
  code.AddRow "var elmnt = document.getElementById(parameter);"
  code.AddRow "var content = 'Hello World!';"
  code.AddRow "elmnt.html = content;"
  code.AddRow "};"
  code.AddRow "Write_something('" + InnerControlID + "')"
  ExecuteJavaScript(String.FromArray(code, EndOfLine.Windows))
End Sub

I suppose it is not Able to identify the HTMLViewer1 content…

Could please somebody help me?

You can’t do that with HTMLViewer in web 2. You will need to put your html into a WebFile and load that url into it.

Greg:
Does this also apply to loading custom JavaScript into WebHTMLViewer 2.0? I realize I can call executeJavaScript() but this only executes (as far as I can tell) during page loading. After loading, no JavaScript function calls, like “onclick”, “onload”, “onbeforeunload”, etc are picked up by the browser even if I have the functions defined in the script.

I have searched the Docs, and don’t see how to do this in 2.0. (Xojo 2020r2.1). Writing a custom control from the ground up is not what I need (and well beyond my reach). I want to leverage the functionaly already built into the HTMLviewer and load my own HTML with included JavaScript code to responde to user actions on the HTMLElements.

Is this possible? How?

Thanks

As I said, put your code into a WebFile and load the WebFile’s url into the HTMLViewer.

Thank you Greg!

Greg thanks. We rely on your knowledge of how the Web 2.0 architecture is written, so many thanks again, even if my question seemed redundant. Your patience with newbies is appreciated.

FWIW, I did instantiate a WebFile object from a disk file in the WebHTMLViewer.Opening event and then tried to load it by calling WebHTMLViewer.GoToURL() as in this code…

var f as folderItem = new FolderItem ("/path/file.html", FolderItem.PathModes.Native)
if f <> nil and f.exists then
    var  wf as WebFile = WebFile.Open(f)
     wf.MIMEtype = "text/html"
     wf.FileName = "file.html"
    GoToURL (wf.URL)
end if

Nothing appeared in the viewer, and the browsers’ inspector showed empty content in the HTMLViewer’s iFrame. Thus my frustration and my question. I was certainly doing something wrong…

After more fruitless tries, I re-read the WebFile docs in the LR. I finally noticed a small detail in the fine print … a WebFile needs to be loaded and retained in a property of the App object…quote:

You need to save the reference. The best way to do this is to use a property of the WebApplication Class

Its only just a few words in the docs, but when I moved the same WebFile instantiation code to the App.Opening event, retained the resulting WebFile object in an App.property, and then called the following code in the WebHTMLViewer.Opening event, it work just as you said.

GoToURL(App.wf.URL)

Now my custom HTML/JavaScripts/CSS loaded and my JavaScripts ran perfectly. I can see the scripts in the browsers debuggerr, set breakpoints, etc. All is well.

I must admit this is a different behaviour in contrast to Desktop controls where I am most familiar with loading resources in the control’s Open event and avoiding populating the global App object.

The difference is subtle, but important, and thus I am replying on this thread for the benefit of future readers who may bump against this same subtly.

I must admit that I do miss all the illustrative example projects that once inhabited the Web 1.0 Extras folder. Seeing is believing…

Thanks again to all for their help.

If you are going to store the property on app, just remember that all of the sessions are going to use that property and they’ll overwrite each other. If the content is going to change from user to user, I suggest putting the property right on the WebPage itself. If it isn’t going to change, make sure you only create it once and that you set the Session property on the WebFile to nil so it is accessible from all sessions.

Greg:

Will do…

and my typo from the prior postiing is

loadURL (App.wf.URL)

Thanks

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.