Matt Fellows
Matt Fellows

Reputation: 6522

Android WebView Javascript not enabled

I have the following code in my activity:

    browser = (WebView)findViewById(R.id.webkit);
    browser.getSettings().setJavaScriptEnabled(true);
    browser.getSettings().setPluginsEnabled(true);
    browser.getSettings().setDomStorageEnabled(true);

    /*if (Build.VERSION.SDK_INT >= 16) {
        browser.getSettings().setAllowFileAccessFromFileURLs(true);
        browser.getSettings().setAllowUniversalAccessFromFileURLs(true);
    }*/

    // Loads html-string into webview
    browser.loadUrl("file:///android_asset/index.html");

THe page loads fine, the css loads fine, the images all load fine. However I also have a local js file. WHich currently only contains an alert message alert("JSEnabled"); and yet the alert never appears. How can I go about diagnosing this?

I'm currently using the emulator to develop the project and have to support back to Gingerbread (2.3.3).

Incidently the same html / js works fine when I use the Browser app on the emulator (Pointing at a remotely served version of the same HTML / js)

I should also point out - all the files are in the assets folder in the project, and I've tried referencing the js as file:///android_assets/main.js and as main.js. I've even tried referencing the remote js file. Nothing works...

Upvotes: 0

Views: 1538

Answers (1)

Jackson Chengalai
Jackson Chengalai

Reputation: 3937

To work your javascript alert please add this to your webview

  webview.setWebChromeClient(new WebChromeClient() {

        @Override
        public boolean onJsAlert(WebView view, String url, String message,
                JsResult result) {

            return super.onJsAlert(view, url, message, result);
        }

    });

Upvotes: 3

Related Questions