最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - WebView returns bad window.innerHeight - Stack Overflow

programmeradmin3浏览0评论

I have an application that makes use of the Android WebView, as well as some JavaScript. When my WebViewClient calls onPageFinished(), I alert my JavaScript to run an initializing method. My JavaScript is enabled and loaded before I hit onPageFinished().

In that JavaScript method I make use of window.innerWidth. However, the value it returns is always wrong and always the same. Regardless of my orientation it reports that the inner width is 320, and the inner height is 240. (Correct width values for portrait and landscape are 360 and 598 respectively.) Anywhere else I access window.innerWidth or window.innerHeight in JavaScript it gives me an accurate number.

What is more puzzling is that if I check the height or width of the WebView directly in my onPageFinished() call using

int height = view.getHeight();

int width = view.getWidth();

then it always returns correctly (although it returns the exact pixel numbers, not the DIP). This makes me think that everything has finished loading with the WebView and so I shouldn't have any problems in my JavaScript.

Any ideas as to what is going on?

Thanks in advance!

I have an application that makes use of the Android WebView, as well as some JavaScript. When my WebViewClient calls onPageFinished(), I alert my JavaScript to run an initializing method. My JavaScript is enabled and loaded before I hit onPageFinished().

In that JavaScript method I make use of window.innerWidth. However, the value it returns is always wrong and always the same. Regardless of my orientation it reports that the inner width is 320, and the inner height is 240. (Correct width values for portrait and landscape are 360 and 598 respectively.) Anywhere else I access window.innerWidth or window.innerHeight in JavaScript it gives me an accurate number.

What is more puzzling is that if I check the height or width of the WebView directly in my onPageFinished() call using

int height = view.getHeight();

int width = view.getWidth();

then it always returns correctly (although it returns the exact pixel numbers, not the DIP). This makes me think that everything has finished loading with the WebView and so I shouldn't have any problems in my JavaScript.

Any ideas as to what is going on?

Thanks in advance!

Share Improve this question edited Sep 6, 2012 at 18:19 Jon asked Sep 6, 2012 at 16:32 JonJon 1,8503 gold badges22 silver badges43 bronze badges 1
  • we have similar issue but get 0 for height and width. – itsme_seenu Commented Aug 16, 2017 at 15:51
Add a ment  | 

4 Answers 4

Reset to default 8

This is because Javascript executes before WebView's View related initialization. And Android WebView returns a 320x240 default value to JS. Execute your JS after some time is ok, like this

function go() {
int height = view.getHeight();
int width = view.getWidth();
};
window.setTimeout(go, 300);

In the end I used the values from webView.getHeight() and webView.getWidth(), adjusted them for screen density, and passed them in as arguments into a javascript method via webView.loadUrl(). This was the only way I could be sure I was getting the right values.

Doing what was suggested in the ments above, checking for the width of 320, and the height of 240 will work great... until you run on a device with those exact dimensions.

After some reading,another solution has occured. in java code, we can use

new Handler().post(new Runnable(){ 
    @Override 
    public void run(){
        webview.loadUrl("...")
    }
}); 

add it to QueueMessage,and delayed the js to execute,and it works.

I used this:

Java code in Activity:

webView.addJavascriptInterface(new WebAppInterface(this), "Android");

...

public class WebAppInterface {
    Context mContext;

    WebAppInterface(Context c) {
        mContext = c;
    }

    public int getWinHeight() {
      return webView.getHeight();
    }
}

JS code:

var winHeight = Android.getWinHeight();
发布评论

评论列表(0)

  1. 暂无评论