问题
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