4

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/john/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?

1
  • Note from the future: cdn.mathjax.org is nearing its end-of-life, check mathjax.org/cdn-shutting-down for migration tips (and perhaps update your post for future readers). Commented Apr 21, 2017 at 7:50

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This also won't work: [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]
[NSURL URLWithString:@"localhost/"] works, so I'll just leave it at that for now. I don't really need a working base url for this app.
Neither using [NSURL URLWithString:@"http://www.google.com"] nor [NSURL URLWithString:@"localhost/"] as base strings is working for me. Anything else you've discovered about this issue?
0

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?

Comments

0

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!

1 Comment

I can't get this to work. Could you take a look at my code? stackoverflow.com/questions/32921577/…

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.