Hi.
I really don't know why this isn't working.
I tried a few things with the code you last posted but as you say the B4A WebView just doesn't seem to actually scroll anything into view.
On my desktop computer the page scrolls fine when viewed with Firefox, IE9 and Safari.
The fact that the scrolling works with Safari led me to think it'd work in B4A as Safari like the B4A WebView is WebKit based - but in B4A it doesn't scroll.
Your webpage HTML doesn't validate - i wondered if this was stopping the scroll working so created a valid XHTML page with my own text.
My webpage validates and uses the same javascript as your page but again the span doesn't scroll into view.
Look at post #3 on this page:
Issue 3883 - google-web-toolkit - scrollIntoView() misbehaves in standards mode (Webkit) - Google Web Toolkit (GWT) - Google Project Hosting.
1. The BODY's clientHeight in standards mode is (in the case of this example with absolutely
positioned content) less than the browser window's viewport height. In fact adding "HTML, BODY {
height: 100%; }" will allow the sample code to work correctly in standards mode.
2. The fact that in Webkit document scrolling is done via the BODY element (i.e.
bodyElement.scrollTop). In Firefox document scrolling is applied via the HTML element.
So i added some CSS style to your page and mine:
<style type="text/css">
html, body {
height:100%;
}
</style>
Scroll still doesn't work.
I added a meta tag (in the 'head' element) and tried that with no success too:
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
I tried with the WebView's ZoomEnabled set to True and False and that didn't work either.
I thought it might be a timing issue - the javascript is trying to scroll the span before the span is initialized.
So i made your btnSearchAgain Button visible and created a new Sub:
Sub btnSearchAgain_Click
Dim javascript As String
javascript="initialise()"
webInterface.execJS(viewHelp, javascript)
End Sub
Note i've added my JSInterface library to the code.
I recompiled the app, the webpage loads and fails to scroll.
I waited half a minute and clicked the button - still no scroll so it's not a timing issue.
Here's the code i'm working with:
Sub Globals
Dim viewHelp As WebView
Dim webInterface As JSInterface
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("SearchHelp")
webInterface.addConsoleInterface(viewHelp)
viewHelp.LoadUrl("file:///android_asset/martin.htm")
End Sub
Sub btnSearchAgain_Click
Dim javascript As String
javascript="initialise()"
webInterface.execJS(viewHelp, javascript)
End Sub
And the webpage javascript:
<script type="text/javascript">
function initialise(){
console.log('initialise start');
var span=document.getElementById('searchResult');
if(typeof span!=='undefined'){
console.log('span found');
span.scrollIntoView();
console.log(span.scrollIntoView);
}
console.log('initialise end');
}
</script>
And the log output:
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
initialise start in file:///android_asset/martin.htm (Line: 14)
span found in file:///android_asset/martin.htm (Line: 17)
function scrollIntoView() {
[native code]
} in file:///android_asset/martin.htm (Line: 19)
initialise end in file:///android_asset/martin.htm (Line: 21)
The javascript throws no errors, the initialise() function successfully runs and the span element is successfully found and it's scrollIntoView() method successfully executed.
Note how the javascript
console.log(span.scrollIntoView); produces the log output:
function scrollIntoView() {
[native code]
} in file:///android_asset/martin.htm (Line: 19)
That indicates (as we already assumed!) that the the WebKit based WebView DOES support the scrollIntoView() method - as do all modern browsers - so the problem isn't there.
I'm not really sure what to suggest now - you've spent time trying to get scrollIntoView to work and there's no logical reason why it doesn't!
Martin.