JavaScript Alert Not Working in Android Webview

JavaScript alert not working in Android WebView

Check this link , and last comment , You have to use WebChromeClient for your purpose.

javascript alert message not working in android application

You need to create WebChromeClient and set to WebiView. Also override onJsAlert method.

     WebChromeClient webChromeClient = new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {

new AlertDialog.Builder(context)
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
})
.create()
.show();

return true;
}
}

webView.setWebChromeClient(webChromeClient);

Webview not showing JavaScript alerts

This seems to be a bug introduced into build 40 of WebView. It is fixed in version 42 as per the issue I've logged.

https://code.google.com/p/chromium/issues/detail?id=478204

Alert is not appear from web view in android?

setContentView(R.layout.main);
WebView webview = (WebView) findViewById(R.id.webview);

WebSettings webSettings = webview.getSettings();

webSettings.setJavaScriptEnabled(true);

webSettings.setBuiltInZoomControls(true);

webview.requestFocusFromTouch();

webview.setWebViewClient(new WebViewClient());
webview.setWebChromeClient(new WebChromeClient());

webview.loadUrl("file:///android_asset/test.html");

this code working perfect and shows me alert box..
and this is my

test.html

<html>
<head>
<script type="text/javascript">
function show_alert()
{
alert("Hello! I am an alert box!");
}
</script>
</head>
<body>

<input type="button" onclick="show_alert()" value="Show alert box" />

</body>
</html>

Custom alert box not loading in android WebView

Well, what you see in Browser isn't actually an "alert box" -- it's an HTTP authentication dialog. In order to handle it, you need to override WebViewClient.onReceivedHttpAuthRequest, see docs.

You can check the relevant code in Android Browser here and here.

How to display alert function from xamarin android button in webview

I have try this but not work =>webView.EvaluateJavascript("javascript: alert('Hi');", null);

You can try to use WebChromeClient() by webview1.SetWebChromeClient(new WebChromeClient());

  protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);

webview1 = FindViewById<WebView>(Resource.Id.webView1);
button1 = FindViewById<Button>(Resource.Id.button1);

button1.Click += Button1_Click;

WebSettings settings = webview1.Settings;
settings.JavaScriptEnabled = true;

webview1.SetWebChromeClient(new WebChromeClient());
webview1.LoadUrl("file:///android_asset/login.html");

}

private void Button1_Click(object sender, System.EventArgs e)
{
//webview1.EvaluateJavascript("javascript: check();", new EvaluateBack());
webview1.EvaluateJavascript("javascript: alert('Hi');", null);
}

The screenshot:

Sample Image

Is there a way to convert Javascript alerts to show as Toast in Android WebView?

Thanks to Moonbloom's answer and this answer also.I managed to come with a solution that works exactly as I wanted. So for anyone who will stumble upon the same issue in the future, the snippet below is for you:


mWebView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message,
final JsResult result) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
result.confirm();
return true;
}
});

Where mWebView is a WebView instance.



Related Topics



Leave a reply



Submit