-1

I want to show a local html/javascript file in the webview. This is my code:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    WebView myWebView = (WebView) findViewById(R.id.webview);
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.loadUrl("file:///android_asset/www/index.html");
    myWebView.setWebViewClient(new WebViewClient());

}

HTML:

<!DOCTYPE html>

<html>

<head>

    <meta charset="utf-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>example</title>

</head>



<body>

<p>Hi guys</p>





<script type="text/javascript">

   alert('aaaa');

</script>

</body>



</html>

HTML works but javascript doesn't work.

Why ?

xRobot
  • 25,579
  • 69
  • 184
  • 304
  • 1
    possible duplicate of [JavaScript alert not working in Android WebView](http://stackoverflow.com/questions/5271898/javascript-alert-not-working-in-android-webview) – user2413972 Sep 17 '15 at 19:48

1 Answers1

0

This is a known problem. Most likely, you will not only works Alert. You have to use WebChromeClient for your purpose.

webView.setWebChromeClient(new WebChromeClient() { 
@Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { 
return super.onJsAlert(view, url, message, result); 
}});
user2413972
  • 1,355
  • 2
  • 9
  • 25