Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-18788] Android: evalJS("document.height") returns undefined on Android 4.4.4

GitHub Issuen/a
TypeBug
PriorityCritical
StatusClosed
ResolutionNot Our Bug
Resolution Date2015-06-16T02:15:13.000+0000
Affected Version/sRelease 3.5.1
Fix Version/sRelease 4.1.0
ComponentsAndroid
Labelsandroid, evaljs, webview
ReporterDavid IAV
AssigneeAshraf Abu
Created2014-07-22T12:09:10.000+0000
Updated2015-06-19T22:10:55.000+0000

Description

Quering a webview to get the height of the content using webview.evalJS("document.height") works fine on Android 4.4.2 (tested on HTC One) and lower devices, but Android 4.4.4 devices (tested on the Nexus 5 and 7) returns undefined instead of the height of the content. Sample code: {noformat} body.addEventListener('load', function(e) { var h = body.evalJS("document.height"); Ti.API.info("Content height: " + h); // returns height on < 4.4.2, undefined on 4.4.4 } {noformat}

Comments

  1. Shuo Liang 2014-08-01

    Ok. No problem. Test case is following:
       var win = Ti.UI.createWindow();
       
       var webview = Ti.UI.createWebView({
           url: 'http://www.google.com'
       });
       win.add(webview);
        
       webview.addEventListener('load',function(e) {
           var l = webview.evalJS("document.height"); 
           Ti.API.info("Context Length " + l);
       });
       
       win.open();
       
  2. Kai De Sutter 2014-11-17

    Hi, I had the very same issue, but as a fix, instead of relying on document.height I now use the following which seems to work perfectly:
       var _height = _html_view.evalJS("Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);");
       Hope this helps!
       
    Kai
  3. Ashraf Abu 2015-06-11

    I think [~kaiiserni] is correct. I'm just looking around and it seems like for Mozilla [https://developer.mozilla.org/en-US/docs/Web/API/Document/height] which is following the HTML5 specification standard: {quote} Note: Starting in Gecko 6.0, document.height is no longer supported. Instead use document.body.clientHeight. See element.clientHeight. {quote} And for Android, [https://developer.android.com/guide/webapps/migrating.html] the WebView was changed on Android 4.4 onwards to Chromium. This is possibly why this behavior changed from Android 4.4.4 onwards. {quote} This change upgrades WebView performance and standards support for HTML5, CSS3, and JavaScript to match the latest web browsers {quote} Therefore, I believe the best answer would be [~kaiiserni]'s answer (thanks for your comment!), which is to use:
       var _height = _html_view.evalJS("Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);");
       
    You shouldn't be using 'document.height'. I believe this should be the answer and the ticket can be resolved as Invalid. [~mrahman] Would this be okay for you?
  4. Ashraf Abu 2015-06-15

    If there's nothing else, I'll close this ticket tomorrow.

JSON Source