Here's a non working(!) example:
Sub Process_Globals
End Sub
Sub Globals
Dim UrlWithAnchor As String="http://whois.domaintools.com/google.com#whoisRecordContainer"
Dim WebView1 As WebView
End Sub
Sub Activity_Create(FirstTime As Boolean)
WebView1.Initialize("WebView1")
Activity.AddView(WebView1, 0, 0, 100%x, 100%y)
Dim WebViewExtras1 As WebViewExtras
WebViewExtras1.addWebChromeClient(WebView1, "")
Activity.AddMenuItem3("Scroll", "MenuItem", Null, True)
WebView1.LoadUrl(UrlWithAnchor)
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub MenuItem_Click
' the menu item can be clicked manually to ensure that the webpage has been rendered
' still document.getElementById('whoisRecordContainer') returns null
Dim Javascript As String="javascript:document.getElementById('whoisRecordContainer').scrollIntoView();"
WebView1.LoadUrl(Javascript)
End Sub
Sub WebView1_PageFinished (Url As String)
' the webpage may have finished loading BUT may not have rendered itself
' in which case there will not exist an element with the id 'whoisRecordContainer'
Log("WebView1_PageFinished: "&Url)
If Url=UrlWithAnchor Then
Dim Javascript As String="javascript:document.getElementById('whoisRecordContainer').scrollIntoView();"
WebView1.LoadUrl(Javascript)
End If
End Sub
The theory is that once the webpage has loaded
and fully rendered itself, there is a DIV element with an id of 'whoisRecordContainer'.
The javascript should get a reference to this DIV and call the DIV's scrollIntoView() method.
The javascript executed in PageFinished fails with the error:
Uncaught TypeError: Cannot call method 'scrollIntoView' of null in null (Line: 1)
document.getElementById('whoisRecordContainer') has returned null and you cannot call the method 'scrollIntoView' of null in null.
Assuming that the webpage has loaded but not yet completely rendered it's HTML elements i added the menu item, ran the project again and waiting a few moments for the webpage to load and render.
Click the menu item and the same error occurs - document.getElementById('whoisRecordContainer') returns null.
I can load the webpage in Firefox and copy/paste the javascript into the Firefox javascript console - that executes the javascript - and it works fine. The HTML DIV with an id of whoisRecordContainer scrolls into view.
Open the webpage in a desktop browser and use the browser's 'View Source' option and you'll find a DIV element with the id 'whoisRecordContainer' in the source.
I'll be thinking - why can't the WebView find the HTML DIV...
Martin.
Note that WebViewExtras is used as it redirects the javascript error messages to the b4a log.
Useful for debugging errors.