WebView不运行loadHTMLString中给出的JavaScript [英] WebView doesn't run JavaScript given in loadHTMLString
问题描述
我不明白为什么这不工作。我有一个test.htm文件坐在我的桌面上,看起来像这样:
< html>< head&
< script type =text / x-mathjax-config>
MathJax.Hub.Config({
tex2jax:{inlineMath:[[$,$],[\\(,\\)]
});
< / script>
< script type =text / javascriptsrc =http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML-full>< / script> < / head>
< body>
这是$ 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>这是$ x ^ 2 $< / body>< / html& baseURL:[NSURL URLWithString:@file:///]];换行符被杀死后,
s被替换为\,并且'被'\'替换,JavaScript无法运行,我只看到纯文本这是$ x ^ 2 $没有$ x ^ 2 $被渲染通过MathJax。
有没有一些秘密,没有真正的webview,请为我执行javascript命令,我错过了吗?
好的,我解决了这个。由于某种原因,web视图不喜欢我的baseURL是file:///。当我将其设置为 http://www.google.com 时,一切正常。我不明白传递它是什么错误file:///。另外使用
[[NSBundle mainBundle] URLForResource:@blankwithExtension:@htm]
无效,但这可能是因为捆绑包中没有blank.htm。
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?
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.
这篇关于WebView不运行loadHTMLString中给出的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!