Skip to content Skip to sidebar Skip to footer

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 vi

Solution 1:

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);

Solution 2:

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.


Solution 3:

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");

Solution 4:

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.


Solution 5:

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.


Post a Comment for "Javascript Not Working In A WebView Activity"