为什么webview中的javascript不起作用?

时间:2013-08-26 11:22:41

标签: android eclipse webview

已加载页面但javascript不起作用!

  protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_poll);

            Device.webViewShowPoll = (WebView) findViewById(R.id.webViewShowPoll);

            Device.webViewShowPoll.getSettings().setJavaScriptEnabled(true);

            //Device.webViewShowPoll.getSettings().setPluginsEnabled(true);
            Device.webViewShowPoll.getSettings().setJavaScriptEnabled(true);
            Device.webViewShowPoll.getSettings().setPluginState(PluginState.ON);
            Device.webViewShowPoll.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
            Device.webViewShowPoll.getSettings().setAllowFileAccess(true);
            Device.webViewShowPoll.addJavascriptInterface(appconnector, "appConnectr");

            try
            {
                Device.webViewShowPoll.loadDataWithBaseURL("file:///android_asset/",
                        Utility.convertStreamToString(getAssets().open("index.htm",
                                AssetManager.ACCESS_BUFFER)), "text/html", "utf-8",""); 
            } 
            catch (IOException e)
            {
                Log.e(getString(R.string.app_name), e.getMessage());
            }
        }
}

AND index.htm是:

<html>
<head>
<script type="text/javascript">
alert('Hello world!');
</script>
</head>
<body>
<p>Hello World!</p>
<p>Hello World!</p>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您尚未设置WebChromeClient来处理JS警报

Device.webViewShowPoll.setWebChromeClient(new WebChromeClient());

JavaScript alert not working in Android WebView