HTMLViewer ScrollPosition

Does anybody know if it is possible to read and write the scroll position of the HTMLViewer (macOS and Windows)? Thanks.

I suspect you’ll need to tell the HTMLViewer to run some javascript to read the position but I can’t remember if javascript can do that.

You’ll want to use the recent additions to HTMLViewer for passing data back and forth. So you can do something like this:

myViewer.ExecuteJavaScript("executeInXojo('scrollPos', window.scrollX, window.scrollY);" )

This will fire the JavaScriptRequest event of the HTMLViewer with the two parameters() values representing the scrollX and scrollY values of the viewport.

If you then want to set the scroll positions, say, if scrollX is greater than 0, you can follow that with:

Function JavaScriptRequest(method As String, parameters() as Variant) Handles JavaScriptRequest as String
  if method = "scrollPos" then
    if parameters(0).IntegerValue > 0 then
      me.ExecuteJavaScript( "window.scroll(0,0);" )
    end if
  end if
End Function

Which will scroll the viewport back to (0,0).

1 Like

You could go further, if you want to subclass the HTMLViewer and add properties for ScrollX and ScrollY, and add an EventListener in JavaScript for the viewport’s scroll events, then assign those properties in the JavaScriptRequest event handler.

1 Like

Thanks a lot. It works perfectly.

Happy to help!