Android的WebView中未启用JavaScript [英] Android WebView Javascript not enabled
问题描述
我有以下的code在我的活动:
浏览器=(的WebView)findViewById(R.id.webkit);
browser.getSettings()setJavaScriptEnabled(真)。
browser.getSettings()setPluginsEnabled(真)。
browser.getSettings()setDomStorageEnabled(真)。 / *如果(Build.VERSION.SDK_INT> = 16){
。browser.getSettings()setAllowFileAccessFromFileURLs(真);
。browser.getSettings()setAllowUniversalAccessFromFileURLs(真);
} * / //加载HTML串入的WebView
browser.loadUrl(文件:///android_asset/index.html);
在页面加载罚款,的CSS加载罚款,图像所有负载罚款。不过我也有一个本地的js文件。目前只包含一个警告信息警报(JSEnabled);
,但绝不会出现警报。我该如何去诊断呢?
我目前使用模拟器来开发该项目,并必须支持回姜饼(2.3.3)。
当我使用的浏览器应用程序的仿真器(在相同的HTML / JS的远程服务的版本指点)顺便说一句相同的HTML / JS工作正常
我还要指出的 - 所有的文件都在该项目的资产文件夹,我已经试过引用的js文件为:///android_assets/main.js和main.js.我甚至试过引用远程js文件。没有什么作品...
要工作,你的JavaScript警告,请添加到您的WebView
webview.setWebChromeClient(新WebChromeClient(){ @覆盖
公共布尔onJsAlert(的WebView视图,URL字符串,字符串消息,
JsResult结果){ 返回super.onJsAlert(查看,网址,邮件,结果);
} });
I have the following code in my activity:
browser = (WebView)findViewById(R.id.webkit);
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setPluginsEnabled(true);
browser.getSettings().setDomStorageEnabled(true);
/*if (Build.VERSION.SDK_INT >= 16) {
browser.getSettings().setAllowFileAccessFromFileURLs(true);
browser.getSettings().setAllowUniversalAccessFromFileURLs(true);
}*/
// Loads html-string into webview
browser.loadUrl("file:///android_asset/index.html");
THe page loads fine, the css loads fine, the images all load fine. However I also have a local js file. WHich currently only contains an alert message alert("JSEnabled");
and yet the alert never appears. How can I go about diagnosing this?
I'm currently using the emulator to develop the project and have to support back to Gingerbread (2.3.3).
Incidently the same html / js works fine when I use the Browser app on the emulator (Pointing at a remotely served version of the same HTML / js)
I should also point out - all the files are in the assets folder in the project, and I've tried referencing the js as file:///android_assets/main.js and as main.js. I've even tried referencing the remote js file. Nothing works...
To work your javascript alert please add this to your webview
webview.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
return super.onJsAlert(view, url, message, result);
}
});
这篇关于Android的WebView中未启用JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!