10ff
10ff

Reputation: 813

Javascript not working in a WebView Activity

I have an Activity that has just a WebView, which contains HTML, CSS and Javascript Code.

It seems that there's a problem with the access of Javascript to the screen size of the view. LogCat says:

(Tag: Web Console): Uncaught TypeError: Cannot call method 'getWidth' of undefined 
at file:///android_asset/Prospekte/grund/html5-player/js/epaper-mobile.js:20

When I look into the js-file, there is: var f=this.body.getWidth();

There curious thing is that sometimes the code works. The epaper is shown well. But most time there's this error.

    setContentView(R.layout.prospekt_layout);
    final Activity activity = this;

    mWebView = (WebView) findViewById(R.id.prospekt_webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setDomStorageEnabled(true);
    mWebView.setWebChromeClient(new WebChromeClient() {
    public void onProgressChanged(WebView view, int progress) {
        activity.setProgress(progress * 1000);
      }
    });

    mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
        {
            // Handle the error
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            view.loadUrl(url);
            return true;
        }
    });   

    mWebView.loadUrl("file:///android_asset/Prospekte/modKachel/mobile.html");    

The layout is:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    >
<WebView
    android:id="@+id/prospekt_webview"
    android:layout_width="900px"
    android:layout_height="900px"
/>
</LinearLayout>

I changed the size of the webView cause I thought that this could be the solution..but it's no working with dp either.

Someone has in idea?

Upvotes: 9

Views: 19871

Answers (5)

Emmanuel
Emmanuel

Reputation: 149

This may fix your problem. You probably mistakenly named the script tag to <scripts> instead of <script> in your HTML code because you were developing in android studio.

Upvotes: 0

Usama Sarwar
Usama Sarwar

Reputation: 9020

Set this setting as well for your webView:

WebSettings settings = webView.getSettings();
settings.setDomStorageEnabled(true);

For Detail refer to answer in the following link: ERROR/Web Console: Uncaught TypeError: Cannot call method 'getItem' of null at http://m.youtube.com/:844

Update: or adding this might help:

webView.loadDataWithBaseURL("fake://fake.com", myString, "text/html", "UTF-8", null);

Upvotes: 15

profuel
profuel

Reputation: 127

You should implicitly enable Javascript execution in your WebView, since this could cause XSS and other vulnerabilities.

web = new WebView(this);
web.getSettings().setJavaScriptEnabled(true);

Also, I prefer to set my WebViewClient via

WebViewClient webViewMainWebClient = new WebViewClient()
{
   // Override page so it's load on my view only
   @Override
   public boolean shouldOverrideUrlLoading(WebView  view, String  url)
   {
       // Return true to override url loading (In this case do nothing).
       return false;
   }
}
web.setWebViewClient(this.webViewMainWebClient);

to let me restrict usage of only my sites.

Upvotes: 4

R Earle Harris
R Earle Harris

Reputation: 983

I had the same problem with my Mo Da Browser project. I added these two lines:

    wvOptions.setBuiltInZoomControls(true);
    wvOptions.setUseWideViewPort(true);

wvOptions is just webview.getSettings() stored in a var. The second line seems to have solved the problem. The first line gives users control over the resulting web page's size. You could add the "open in overview" option if you liked the result. That didn't help me so I used the zoom option.

Upvotes: 1

Bastien
Bastien

Reputation: 11

Got the same problem on android 3.2. A solution that works for me (pretty ugly) is to put a sleep and call another loadUrl, try with more time if 300ms is not sufficient:

myWebView.loadUrl("file:///android_asset/gabarit.html");
try {
    Thread.sleep(300);
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
myWebView.loadUrl("file:///android_asset/gabarit.html");

Upvotes: 1

Related Questions