鉴于此页面: http://music.10086.cn/newweb/qk/guoso/1C5DB9AC5C346441/t/6.html
启用JavaScript的WebView可以使用loadUrl()正确打开它并呈现JavaScript。
但是,如果页面内容是使用HTTP客户端单独加载的,并且使用基本URL设置给loadDataWithBaseUrl(),则JavaScript无法正常工作:
// store http://music.10086.cn/newweb/qk/guoso/1C5DB9AC5C346441/t/6.html into data
webView.loadUrl("http://music.10086.cn/", data, "text/html", "UTF-8", null);
我怀疑远程脚本没有加载。如何让WebView记录下来?
这是在Android Ice Cream Sandwich 4.0.3上。
答案 0 :(得分:2)
有两件事需要仔细检查:
<uses-permission android:name="android.permission.INTERNET" />
AndroidManifest.xml
webView.getWebSettings.setJavaScriptEnabled(true);
以下代码适用于我,从http://code.jquery.com/加载jquery:
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.setWebChromeClient(new MyWebChromeClient());
String mimeType = "text/html";
String encoding = "utf-8";
String html = "<html> "
+ "<script src='jquery-1.10.1.min.js'></script>"
+ "<script language='javascript'>"
+ "function sayHello() { "
+ "alert(typeof($)); "
+ "} </script> "
+ "<body> "
+ " <a onClick='sayHello()'><div style='width:80px; "
+ " margin:0px auto; " + " padding:10px; "
+ "text-align:center; " + " border:2px solid #202020;' > "
+ "<br> "
+ " Click me! " + "</div></a> " + " </body>" + " </html>";
mWebView.loadDataWithBaseURL("http://code.jquery.com/", html, mimeType, encoding,null);
}