Enabling General JavaScript in Webviewclient

Enabling general JavaScript in WebViewClient

I don't know what your exact problem is, but i can enable the JavaScript and a custom WebViewclient without any problem:

WebView vistaWeb = (WebView) findViewById(R.id.webview);
vistaWeb.setWebChromeClient(new MyCustomChromeClient(this));
vistaWeb.setWebViewClient(new MyCustomWebViewClient(this));
vistaWeb.clearCache(true);
vistaWeb.clearHistory();
vistaWeb.getSettings().setJavaScriptEnabled(true);
vistaWeb.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

How to load Java Script in Web View in android for multiple windows?

Actually you are try to use single web-url and your site may contain other site url or web pages within single page,..

You should enable javascript and multiple windows true

you have to change web view settings

try this,

    webView.getSettings().setJavaScriptEnabled(true); // enable javascript

webView.getSettings().setSupportMultipleWindows(true); //suport multiple windows

Android:How to add support the javascript alert box in WebViewClient?

Implement the WebViewClient and WebChromeClient both like this

webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

progressBar = new ProgressDialog(this);
progressBar.setCancelable(true);
progressBar.setMessage("Loading...");
progressBar.show();

final Context mapp = this;

webView.setWebViewClient(new WebViewClient() {

public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i("TEST", "Processing webview url click...");
// to kill activity
view.loadUrl(url);
return true;
}

public void onPageFinished(WebView view, String url) {
Log.i("TEST", "Finished loading URL: " + url);
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}........

then implement the WebChromeClient for
javascript alert,confirm and prompt

 webView.setWebChromeClient(new
WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result) {
new AlertDialog.Builder(mapp)
.setTitle(R.string.title_dialog_alert)
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
}).setCancelable(false).create().show();

return true;
}

@Override
public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
new AlertDialog.Builder(mapp)
.setTitle(R.string.title_dialog_confirm)
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
}).setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.cancel();
}
}).create().show();
return true;
}

@Override
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, final JsPromptResult result) {
final LayoutInflater factory = LayoutInflater.from(mapp);
final View v = factory.inflate(R.layout.javascript_prompt_dialog, null);

((TextView)v.findViewById(R.id.prompt_message_text)).setText(message);
((EditText)v.findViewById(R.id.prompt_input_field)).setText(defaultValue);

new AlertDialog.Builder(mapp)
.setTitle(R.string.title_dialog_prompt)
.setView(v)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = ((EditText)v.findViewById(R.id.prompt_input_field)).getText().toString();
result.confirm(value);
}
})
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
result.cancel();
}
})
.setOnCancelListener(
new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
result.cancel();
}
})
.show();

return true;
};

});

for more details please check out http://code.google.com/p/mosembro/source/browse/trunk/src/com/lexandera/mosembro/Mosembro.java

how to display a WebView only after a javascript code is done?

You can try injecting a JavaScript interface and call that interface method when you reach at the last image in the loop to change WebView visibility.

Something like this:

webview.addJavascriptInterface(this, "visibility");

and call this interface from your JavaScript like this:

var imgs = document.getElementsByTagName('img');
for (i = 0; i < imgs.length; i++) {
imgs[i].style.display = 'none';
if (i == imgs.length-1) {
visibility.changeVisibility();
}
}

Now define changeVisibility() with @JavascriptInterface annotation in that activity in which you have added javascriptInterface like this:

@JavascriptInterface
public void changeVisibility() {
runOnUiThread(() -> webview.setVisibility(View.VISIBLE));
}

overriding javascript function in a webview

You need

  1. grab html page source
  2. Edit it. (add into html source you grabbed, easy way : replace("<head>","<head>"+"your code");
  3. load edited String of HTML using webview.loadDataWithBaseURL("", string, "text/html", "utf-8", "");


Related Topics



Leave a reply



Submit