Android 4.1 webview javascript not working.

南笙酒味 提交于 2020-01-01 18:20:32

问题


I am having a function calling javascript:alert('load') attempting to show an alert box. It is not working even I have already setJavaScriptEnabled(true);

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    WebView wvMyWeb = (WebView)findViewById(R.id.webView1);
    wvMyWeb.setWebViewClient(new MyWebViewClient());
    wvMyWeb.getSettings().setJavaScriptEnabled(true);

    switch (item.getItemId()) {
        case R.id.menu_save:
            alertbox("Setting","Save Clicked");
            wvMyWeb.loadUrl("http://ec.m.zkiz.com");
            return true;
        case R.id.menu_load:
            wvMyWeb.loadUrl("javascript:alert('load')");
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

What am I missing?


回答1:


You did not specify a chrome client, the WebChromeClient will fit your needs. Simply add this to your webview :

webView.setWebChromeClient(new WebChromeClient());

If you'd like to handle alert events in a particular way, you can also do :

webView.setWebChromeClient(new WebChromeClient() { 
    @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
        /* Do whatever you need here */
        return super.onJsAlert(view, url, message, result); 
    } 
});


来源:https://stackoverflow.com/questions/15829501/android-4-1-webview-javascript-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!