Qing
Qing

Reputation: 1411

Unable to execute Js code in WebView in Cordova Project

I have two activity, one main activity(A) is an CordovaActivity, then I use intent to start another activity(B), in B i have an WebView(not CordovaActivity), and after I use this webview to load a simple webpage (alert something), I found the js code is not executed at all, even if I enable javascript by calling setttings.setJsenabel(true);

I start activity B from A enter image description here

Load Url from webview in Activity B

enter image description here

simple web page

enter image description here

in the device, it does not alert anything

enter image description here

However, if I change the webview to CordovaWebView instead of the original Android native one, it works.....

enter image description here

enter image description here

Upvotes: 0

Views: 829

Answers (1)

Mikhail Naganov
Mikhail Naganov

Reputation: 6871

That's because plain WebView doesn't support showing alerts by itself. You need to provide a WebChromeClient to it that implements WebChromeClient.onJsAlert method. For example:

mywebView.setWebChromeClient(new WebChromeClient() {
    @Override
    public boolean onJsAlert(
            WebView view, String url, String message, final JsResult result) {
        new AlertDialog.Builder(view.getContext())
            .setTitle("Alert")
            .setMessage(message)
            .setPositiveButton("Ok",
                new AlertDialog.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        result.confirm();
                    }
                }).setCancelable(false).create().show();
        return true;
    }
});

Upvotes: 1

Related Questions