4

Given this page: http://music.10086.cn/newweb/qk/guoso/1C5DB9AC5C346441/t/6.html

A WebView with JavaScript turned on can open it properly with loadUrl() and render the JavaScript.

However, if the content of the page is loaded separately using a HTTP client, and given to loadDataWithBaseUrl() with the base URL set, the JavaScript does not work properly:

// 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);

I suspect that the remote scripts are not loading. How can I make the WebView log this?

This is on Android Ice Cream Sandwich 4.0.3.

2
  • 1
    I have seen the same thing with local JS and CSS. Maybe some security enforcement that they introduced in ICS? Commented Oct 20, 2013 at 8:15
  • Thanks for confirming. Yes, perhaps. But it will take up too much time to go through the Android WebKit source code to debug. Commented Oct 20, 2013 at 11:14

1 Answer 1

2

There are a couple of things to double-check:

  1. You have included <uses-permission android:name="android.permission.INTERNET" /> in AndroidManifest.xml
  2. You have called webView.getWebSettings.setJavaScriptEnabled(true);
  3. Your HTML is well-formed

The following code worked for me, loading jquery from http://code.jquery.com/:

@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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.