James
James

Reputation: 14121

javascript not working in android

I have a webview in my program. I loaded a string into this webview that contains javascript function. When i loaded it in the emulator it's not working i.e if I write a simple alert the webView won't display the alert. I have enabled the javascript. But then also its not working. what may be the reason? Please help Thankyou

Upvotes: 0

Views: 935

Answers (1)

Dan
Dan

Reputation: 3884

Alerts will not normally work in a WebView. You will need to write the code to do that yourself. You can easily do that by implementing your own version of WebChromeClient. In otherwords:

class ChromeClient extends WebChromeClient {
    public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
        Toast.makeText(view.getContext(), message, Toast.LENGTH_SHORT).show();
    }
}

... 
mWebView.setWebChromeClient(new ChromeClient());

Another thing I would recommend is implementing the onConsoleMessage as well. This way you can just use "console.log" in your JavaScript and have it directed to Toast or the Android Log.

Upvotes: 2

Related Questions