WebView does not start JavaScript specified in loadHTMLString

I do not understand why this does not work. 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 downloads this from my desktop via

 [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"file:///Users/john/Desktop/test.htm"]]]; 

which works great. Page loading is done by javascript MathJax and that $ x ^ 2 $ turns into a cool 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 the exact same web page after newlines are killed and \ replaced by "\" and "replaced by" \ ", JavaScript does not start and I just see plain text. This is $ x ^ 2 $ without mappings of $ x ^ 2 $ via MathJax.

Is there some secret command "no really webview, do javascript for me" that I am missing?

+4
source share
3 answers

Well, I "decided" that. For some reason, the web view doesn't like it when my baseURL is the file: ///. When I install it at http://www.google.com everything works fine. I do not understand what is wrong with the file transfer: ///. Also using

 [[NSBundle mainBundle] URLForResource:@"blank" withExtension:@"htm"] 

does not work, but it may be because the package does not have an empty .htm.

+3
source

I had the same problem, my solution was to insert javascript into the html file. When I had this in a separate javascript file, for some reason it refused to run javascript. The javascript file was included in the xcode project.

Still wondering why individual javascript files don't work, any ideas?

0
source

I had the same problem. I solved this by saving โ€œtextHTMLโ€ in a file in the โ€œDocumentโ€ folder and downloading MathJax from the resources folder (in my case or in this case when downloading from the MathJax website), then it worked. If your MathJax files are local (for example, my application), you need to change the script address to HTML text for this script (be careful when you write your address because the device works on a case-sensitive other than the simulator):

 <script type="text/javascript" src="../YOUR_APPLICATION_NAME.app/MathJax/MathJax.js"></script> 

Details 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!"); 

Hope this works for you!

0
source

Source: https://habr.com/ru/post/1345140/


All Articles