B4J Question Internal link in Webview

Informatix

Expert
Licensed User
Longtime User
I'm unable to make the internal links work as expected in a Webview. What am I doing wrong?
Here's a little example to try. Click on Test. Nothing happens.
B4X:
Dim Contenu As String = "<HTML><HEAD></HEAD><BODY><A HREF='#test'>Test</A>"
For i = 0 To 100
     Contenu = Contenu & "<BR/>"
Next
Contenu = Contenu & "<A name='test'>I want to go there</A></BODY></HTML>"
HTMLView.LoadHtml(Contenu)
 

Informatix

Expert
Licensed User
Longtime User
I found a workaround. Add id='test' to the anchor in the example above and call this code on a link click:
B4X:
Dim JO As JavaObject = HTMLView
Dim WebEngine As JavaObject = JO.RunMethodJO("getEngine", Null)
WebEngine.RunMethod("executeScript", Array("document.getElementById('test').scrollIntoView()"))
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I see it too. It looks like a problem in the native WebView.

A solution based on jQuery with scrolling animation:
B4X:
  Dim html As String = $"
   <html>
   <head>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
   </head>
   <body>
   <a id="jump" href="tt">Jump</a>
   <script type="text/javascript">
       $("#jump").click(function() {
      $('html,body').animate({
      scrollTop: $("#test").offset().top},
       'slow');
       });
   </script>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>

   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <br/>
   <div id="test">test</div>
   </body></html>
   "$
   WebView1.LoadHtml(html)
 
Upvote 0
Top