DesktopHTMLViewer and scrolling

I have an app that has a TextArea into which I type, say, HTML or Markdown text. There is also a DesktopHTMLViewer that displays the contents of the TextArea. The problem is if I make a change in the TextArea and load the text into the DesktopHTMLViewer, it always scrolls to the top of the page. Hence if you make a change in the text that affects the HTML view off the bottom of the Viewer, it requires scrolling down to find the place I was looking at previously. Kinda annoying.

Is there any way of getting and setting the DesktopHTMLViewer’s vertical scroll position?

Hi Paul,
I’ve created a simple example project for you.

So you need to get and cache the scroll position when it happens. For this, you need to:

  1. Bind to the scroll event in JavaScript on DesktopHTMLViewer.DocumentComplete
  2. Capture the resulting change and send it back to the Xojo app using executeInXojo()
  3. Store the current scroll Y position in the DesktopHTMLViewer.JavaScriptRequest event
  4. Restore the scroll Y position using the window.scroll(x,y) JavaScript function in DesktopHTMLViewer.DocumentComplete

There are other ways to achieve this which don’t result in flashing and so much communication back and forth between JavaScript and Xojo, but it seems like you’re reloading the entire document rather than modifying the contents of an element on the page.

Hope this helps.

1 Like

That will do the trick. Thanks a million.