JavaScript – alerts in embedded WebView

JavaScript alert() does not work when you embed WebView and load HTML pages in your application. Please give me an example

resolvent:

The default webchromeclient implemented by the embedded browser will discard JavaScript alerts. You should overwrite the webchromeclient implementation with your own version, which also allows you to create your own custom alerts instead of the default alerts, as shown below:

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;
    }
}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>