1

I'am newbie in Android programming and I want to us jqMath to display some math formulas in WebView.

Here is my code:

    WebView webView = (WebView)findViewById(R.id.webView1);
WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
String js = "<html><head>"
    + "<link rel='stylesheet' href='jqmath-0.4.0.css'>"
    + "<script src='jquery-1.4.3.min.js'></script>"
    + "<script src='jqmath-etc-0.4.0.min.js'></script>"
    + "</head><body>"
    + "<script>var s = '$ax^2+bx+c=0$ with $a≠0$';M.parseMath(s);document.write(s);</script></body>";
webView.loadData(js,  "text/html",  "UTF-8");

What is the problem of this code?,

UPDATE well my problem have solved, but I also changed loadData function to loadDataWithBaseURL I mention just for reference if anyone else have same problem

VividD
  • 10,456
  • 6
  • 64
  • 111
Arash
  • 383
  • 4
  • 16
  • This use of M.parseMath(s) is incorrect, and is being copied to other stackoverflow pages/questions. M.parseMath() should be passed a DOM node, not a string, and is only necessary if you need to change mathematics after the page is fully loaded, e.g. due to user interaction. The call here is a no-op. For anyone copying this code, get rid of the – Dave Barton Nov 29 '15 at 21:57
  • Also the js variable is really html, not js. – Dave Barton Nov 29 '15 at 21:57

3 Answers3

6

You need to specify the path correctly for the css,js files as following:

WebView webView = (WebView)findViewById(R.id.webView1);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
String path="file:///android_asset/";
String js = "<html><head>"
+ "<link rel='stylesheet' href='"+path+"jqmath-0.4.0.css'>"
+ "<script src='"+path+"jquery-1.4.3.min.js'></script>"
+ "<script src='"+path+"jqmath-etc-0.4.0.min.js'></script>"
+ "</head><body>"
+ "<script>var s = '$ax^2+bx+c=0$ with $a≠0$';M.parseMath(s);document.write(s);</script></body>";
webView.loadData(js,  "text/html",  "UTF-8");

note that the files should be in assets folder.

Khalid Taha
  • 3,183
  • 5
  • 27
  • 43
  • I too faced a similar issue. The path can't be short. Without path, rest of the code works, but fails to load the .js file, and fails to render the formula. – Nagaraju Gajula Oct 16 '16 at 14:15
1

the location of js lib in assets folder does not match your src declaration assuming that you placed the js libs in assets/js

Fred Grott
  • 3,505
  • 1
  • 23
  • 18
0

After specifying the path like Khalid said, the following worked for me

webView.loadDataWithBaseURL( "file:///android_asset/" ,js,  "text/html",  "UTF-8", null);

instead of webView.loadData method.

Community
  • 1
  • 1