views:

186

answers:

1

When embedding WebView in an application and loading html-pages in it, JavaScripts alert() do not work.Give me an example pls

+3  A: 

The default WebChromeClient implemented by the embedded browser will discard javascript alerts, you should override the WebChromeClient implementation with your own version, this also allows you the ability to create your own custom alerts in place of the default one like so:

browser.setWebChromeClient(new MyWebChromeClient());

...

final class MyWebChromeClient extends WebChromeClient {
    @Override
    public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
        Log.d(LOG_TAG, message);
        new AlertDialog.Builder(view.getContext()).setMessage(message).setCancelable(true).show();
        result.confirm();
        return true;
    }
}
Quintin Robinson