WebView 不运行 loadHTMLString 中给出的 JavaScript
我不明白为什么这不起作用。我的桌面上有一个 test.htm 文件,如下所示:
<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>
我有一个 WebView,它从我的桌面加载该文件
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"file:///Users/john/Desktop/test.htm"]]];
,效果很好。页面加载,MathJax javascript 运行,$x^2$ 变成一个漂亮的排版数学脚本。
但是,如果我尝试这样做:
[[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:///"]];
在换行符被终止并且 \ 被替换为“\”并且“被替换为 '\”' 后加载完全相同相同的网页,则 JavaScript无法运行,我只看到纯文本 This is $x^2$ ,而没有通过 MathJax 渲染 $x^2$ 。
是否有一些我缺少的秘密“没有真正的 webview,请为我执行 javascript”命令?
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^2lt;/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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,我“解决”了这个问题。由于某种原因,网络视图不喜欢我的baseURL是file:///。当我将其设置为 http://www.google.com 时,一切正常。我不明白传递 file:/// 有什么问题。另外使用
不起作用,但这可能是因为捆绑包中没有blank.htm。
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
did not work, but that may be because there is no blank.htm in the bundle.
我遇到了同样的问题,我的解决方案是将 javascript 嵌入到 html 文件中。当我将它放在一个单独的 javascript 文件中时,由于某种原因它拒绝运行 javascript。 javascript 文件包含在 xcode 项目中。
仍然想知道为什么单独的 javascript 文件不起作用,有什么想法吗?
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?
我也有同样的问题。
我通过将“textHTML”保存在文档文件夹中的文件中并从资源文件夹加载MathJax(在我的情况下或在本例中从MathJax的网站加载)来解决它,然后它就工作了。
如果您的 MathJax 文件是本地的(就像我的应用程序),您需要将 HTML 文本上的脚本地址更改为此脚本(编写地址时要小心,因为设备区分大小写,与模拟器不同):
详细信息在这里:
希望对你有用!
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):
It's detail is here:
I hope, it works for you!