JavaScript not working in Android Webview?

I'm trying to make an Android version of a relativly simple iOS app that uses a webview, some buttons and then relies on javascript calls to a CMS.

But I'm stuck at a pretty early point of development: The webview doesn't function with javascript.I've read a lot of posts about how to enable JS in an Android webview, but no luck so far.

Below is some of my code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mWebView = (WebView) findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setWebChromeClient(new WebChromeClient());
    mWebView.setWebViewClient(new HelloWebViewClient()
    {
        @Override  
        public void onPageFinished(WebView view, String url)  
        {  
            //Calling an init method that tells the website, we're ready 
            mWebView.loadUrl("javascript:m2Init()");
            page1(mWebView);
        }  
    });
  mWebView.loadUrl("http://my_url/mobile/iphone//app.php");  
}

private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
        mWebView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

public void page11(View view)
{
    mWebView.loadUrl("javascript:m2LoadPage(1)");
}

What am I doing wrong here? The URL is working perfectly in my iOS app, and in a browser. But not in my app!

Please tell me it's something obvious...


FIXED! Spurred on by the error, I found out that I needed to set

setDomStorageEnabled(true)

for the webview settings.

Thanks for your help Stephan :)


In case something with WebView on Android does not work, I always try to make sure I set these crazy flags such as,

    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setDomStorageEnabled(true);
    webSettings.setLoadWithOverviewMode(true);
    webSettings.setUseWideViewPort(true);
    webSettings.setBuiltInZoomControls(true);
    webSettings.setDisplayZoomControls(false);
    webSettings.setSupportZoom(true);
    webSettings.setDefaultTextEncodingName("utf-8");

I wonder why these are not set by Default, who would expect webpages without javascript content nowadays, and whats the use having javascript enabled when DOM is unavailable unless specified. Hope someone filed this as a bug or improvement/feature-request already and the monkeys are working on it.

and then there is deprecated stuff rotting somewhere, like this:

webView.getSettings().setPluginState(PluginState.ON);

All this for loading webpages inside app.

On iOS, its all so simple - Swift 3.0

private func openURLWithInAppBrowser(urlString:String) {
    guard let url = URL(string:urlString) else {
        return
    }
    let sfSafari = SFSafariViewController(url:url)
    present(sfSafari, animated: true, completion: nil)
}

Loading javascript in webview

webView.getSettings().setDomStorageEnabled(true);

Did you enable the right internet permission in the manifest? Everything looks fine otherwise. By any chance, have you also tested this code on an actual Android phone? And not just on the emulator?

Here is a good tutorial on a slightly different approach. You may want to try that one to see if it works for you.


Add the following lines of code in your MainActivity.java

It helped me to enable js

  webSetting.setJavaScriptEnabled(true);
  webView.setWebChromeClient(new WebChromeClient());
  webView.setWebViewClient(new WebViewClient());

do not forget about this permission in AndroidManifest file.

<uses-permission Android:name="Android.permission.INTERNET" />