0

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...

1 Answer 1

3

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

    });
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Looks like it wasn't necessarily the best test to check JS was working then ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.