@JohnC Thank you for providing this helpful information in your signature. On my mobile device the browser does not show the users signature. It was helpful to point me to it in your second post.
@drgottjr Your solution is a very clever hack to make it work. Thank you for sharing it.
my solution answers your question. it does work.
In addition to the solution provided I was just wondering why the Webview seems to behave different when loading a page with a fragment identifier from a remote server via http:// or https:// protocol from loading it from local file system using file:// protocol.
Thanks to
@kps I realized it was my fault. I thought it would be a good idea to load a demo page from b4x.com to show the different behavior. But it turns out that the saved page was broken (it was actually no html but a text file).
When using a simple local html file with both versions of fragment identifiers (the old anchor technique in line 9 and new id technique in line 12) everything works fine. For this simple html local file the Webview behaves like expected. It scrolls to it!
Here is the html file used:
<!doctype html>
<html>
<head>
<title>Simple page</title>
</head>
<body>
<h1>Simple page</h1>
<p>Lorem ipsum …</p>
<a name="scroll1"></a>
<h2>Scroll here by anchor</h2>
<p>Lorem ipsum dolor …</p>
<h2 id="scroll2">Or scroll here by id</h2>
<p>Lorem ipsum dolor sit …</p>
<h2>Do not scroll here</h2>
<p>Lorem ipsum ...</p>
</body>
</html>
This is the code that works:
' Using localy saved file
' Does not work
WebView1.LoadUrl(xui.FileUri(File.DirAssets, "simple.html#scroll1")) ' File is not found
' Does work
WebView1.LoadUrl(xui.FileUri(File.DirAssets, "simple.html") & "#scroll1") ' Scrolls to URL Fragment using an anchor
' Does work
Dim strDir As String
strDir = rp.GetSafeDirDefaultExternal("html")
File.Copy(File.DirAssets, "simple.html", strDir, "simple.html")
WebView1.LoadUrl("file://" & File.Combine(strDir, "simple.html#scroll2")) ' Scrolls to URL Fragment using an id
Updated b4a project is attached to this post.
The conclusion is, that we found no bug in WebView but we might have a Bug in xui.getFileUri() since it does not handle a fragment identifier although the name of this operation refers to URI standard (see
RFC 2396 section 4.1). But reading the b4x documentation of getFileUri() operation it expects a FileName and therefore XUI considers a fragment to be part of the filename instead. We probably need to be aware of it.
Thanks to everyone for helping with this problem!
Thomas