启用在WebViewClient一般的JavaScript [英] Enabling general JavaScript in WebViewClient
问题描述
虽然搜索在谷歌的答案,似乎我不是唯一一个坚持,这似乎是不可能解决问题的。
While searching for an answer in google, it seems that I'm not the only one stuck with a problem that seems impossible to solve.
我已经成功地创建一个自定义WebViewClient一个的WebView - 这使得我能够有一个processdialog并显示错误信息,如果无法加载一个网址
I've managed to create a WebView with a custom WebViewClient - this makes it possible for me to have a processdialog and show an error message if an URL couldn't be loaded.
但是,这将创建JavaScript的一个问题。我装的URL有一些JavaScript它改变一些HTML元素的CSS样式(显示或隐藏元素)或重定向到另一个位置的onclick - 或者甚至想的显示一个警告框。但是,通过使用WebViewClient它们没有一个是工作。
But this creates a problem with JavaScript. The URL I'm loading has some JavaScript which changes some HTML elements CSS styles (showing or hiding element) or redirects to another location onclick - or maybe even want's to show an alert box. But by using the WebViewClient none of those are working.
这是我如何加载一个网页:
This is how I load a page:
public void loadUrl(String url)
{
final ProgressDialog dialog = ProgressDialog.show(myActivity.this, "", getString(R.string.loading), true);
final WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setVerticalScrollBarEnabled(false);
myWebView.setHorizontalScrollBarEnabled(false);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
Toast.makeText(myActivity.this, url, Toast.LENGTH_SHORT).show(); //Debugging purposes
if (url.endsWith(".mp4"))
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), "video/mp4");
view.getContext().startActivity(intent);
}
else
{
view.loadUrl(url);
}
return true;
}
public void onPageFinished(WebView view, String url)
{
//Toast.makeText(myActivity.this, "Oh no!", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
Toast.makeText(myActivity.this, description, Toast.LENGTH_SHORT).show();
String summary = "<html><body><strong>" + getString(R.string.lost_connection) + "</body></html>";
myWebView.loadData(summary, "text/html", "utf-8");
}
}); //End WebViewClient
myWebView.loadUrl(url);
}
这也许可以在一个更聪明的方法做,但我新的Java和Android开发...
This could probably be done in a smarter way, but I'm new at both Java and Android development...
是否有可能对我来说,启用JavaScript来实现WebViewClient呢? 卸下WebViewClient解决了这个问题,但后来当页面错误或完成加载,我不能赶上事件。
Is it possible for me to enable the JavaScript for the WebViewClient at all? Removing the WebViewClient solves the problem, but then I can't catch events when the page errors or is finished loading.
推荐答案
我不知道您的具体问题是什么,但我可以启用JavaScript和自定义的 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 = (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);
我不知道如果你的最后
的声明可能会导致这个问题。
I wonder if your final
declarations could be causing that problem.
这篇关于启用在WebViewClient一般的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!