Android Question Display web page at a specific point

lock255

Well-Known Member
Licensed User
Longtime User
I have the need to call a page in my WebView but in an exact spot. In this way, step directly to the content I want, how can I do?
 

DonManfred

Expert
Licensed User
Longtime User
you should explain more details... Normally i would say: call the right url
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
www.LoadUrl("http://www.b4x.com/android/forum/threads/display-web-page-at-a-specific-point.40565/#post-242674")

This opens a url in a webview and scroll to the anchor post-242674

If you wnt to know more about see this link for example or search google about "html anchor"
 
Last edited:
Upvote 0

warwound

Expert
Licensed User
Longtime User
@DonManfred
On your computer's browser works, but on a webview, it tells me that the page does not exist.

It's a known problem with the android WebView, have a look at this search: https://www.google.co.uk/search?q=android+webview+anchor+links&ie=UTF-8&oe=UTF-8.
Some devices 'just work' some fail to scroll to the anchor.
Looks like there's no definite solution - trial and error might produce a solution but there's no way to know that solution will work on all devices and all versions of android.

There is a javascript command to scroll the webpage so that a certain element is made visible: scrollInfoView but i know that there used to be problems using this in an android WebView.
Again on some devices the javascript seemed to work on others it failed.

If i was you i'd research the problem with anchor links and see if you can find a solution, otherwise experiment and see if you can execute javascript to bring the required part of the page into view.

Martin.
 
Upvote 0

lock255

Well-Known Member
Licensed User
Longtime User
If i was you i'd research the problem with anchor links and see if you can find a solution, otherwise experiment and see if you can execute javascript to bring the required part of the page into view.

Thanks for relply Martin (@warwound).
Unfortunately we do not really know how to work with your last suggestion. You may report me a guide or write a code that could help me to solve a problem?
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Here's a non working(!) example:

B4X:
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.
 

Attachments

  • WebViewAnchor.zip
    6.2 KB · Views: 148
Upvote 0

lock255

Well-Known Member
Licensed User
Longtime User
@warwound
Thanks to your code Martin, I understand the operation, but I've tested both in the emulator and on my phone but it does not work (even in a manual).
I'm sorry because I really need this feature for my project :(
 
Upvote 0
Top