I don't understand why this isn't working. I have a test.htm file sitting on my desktop that looks like this:
<html><head>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {inlineMath: [["$","$"],["\\(","\\)"]]}
});
</script>
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML-full"></script></head>
<body>
This is $x^2$
</body></html>
and I have a WebView that is loading this from my desktop via
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"file:///Users/joh开发者_高级运维n/Desktop/test.htm"]]];
which works fine. The page loads, the MathJax javascript runs, and that $x^2$ is turned into a nice typeset math script.
However, if I try to do this:
[[webView mainFrame] loadHTMLString:@"<html><head><script type=\"text/x-mathjax-config\">MathJax.Hub.Config({tex2jax: {inlineMath: [[\"$\",\"$\"],[\"\\\\(\",\"\\\\)\"]]}});</script><script type=\"text/javascript\" src=\"http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML-full\"></script></head><body>This is $x^2$</body></html>" baseURL:[NSURL URLWithString:@"file:///"]];
which loads exactly the same webpage after newlines are killed and \'s are replaced with "\" and "'s are replaced with '\"', the JavaScript fails to run, and I just see the plain text This is $x^2$ without the $x^2$ being rendered via MathJax.
Is there some secret, "no really webview, please execute javascript for me" command that I am missing?
Ok, I "solved" this. For some reason the web view doesn't like my baseURL being file:///. When I set it to http://www.google.com, everything works fine. I don't understand what is wrong with passing it file:///. Additionally using
[[NSBundle mainBundle] URLForResource:@"blank" withExtension:@"htm"]
did not work, but that may be because there is no blank.htm in the bundle.
I had the same problem, my solution was to embed the javascript in the html file. When I had it in a separate javascript file then for some reason it refused to run the javascript. The javascript file was included in the xcode project.
Still wondering why separate javascript files doesn't work, any ideas?
I had the same problem too. I solved it by saving "textHTML" on file in the Document folder and loading MathJax from resource folder (in my case or in this case loading from MathJax's website), then it worked. If your MathJax files are local (like my app), you need to change your script address on HTML text to this script (Be careful when you write your address, because device works on case sensitive, different from simulator):
<script type="text/javascript" src="../YOUR_APPLICATION_NAME.app/MathJax/MathJax.js"></script>
It's detail is here:
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
path = [path stringByAppendingString:@"/myHTML.html"];
NSString *textHTML = @"<html>..."; // change to your HTML string
NSError *error;
BOOL ok = [textHTML writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
if(ok){
NSLog(@"Write done!");
NSURL *url = [NSURL fileURLWithPath:path isDirectory:NO];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
}else
NSLog(@"Error!");
I hope, it works for you!
精彩评论