在android 4.4上加载资产文件时没有捕获ReferenceError

时间:2013-12-14 10:43:48

标签: android webview mathjax

我正在使用以下MathJax应用代码。 http://cs.jsu.edu/wordpress/?p=498#comment-217

在以下功能中,我正在尝试从资产目录加载文件。

public static boolean makeMathView_new(WebView webview) {
            webview.getSettings().setRenderPriority(RenderPriority.HIGH);
            webview.getSettings().setJavaScriptEnabled(true);
            webview.loadDataWithBaseURL("http://bar", "<script type='text/x-mathjax-config'>"
                    +"MathJax.Hub.Config({ messageStyle: 'none', showMathMenu: false, jax: ['input/TeX','output/HTML-CSS'], "
                    +"extensions: ['tex2jax.js'],"         
                + "'HTML-CSS': { scale: 100 },"              
                    +"TeX: { extensions: ['AMSmath.js','AMSsymbols.js','noErrors.js','noUndefined.js'] } });</script>"
                +"<script type='text/javascript' src='file:///android_asset/MathJax/MathJax.js'></script>" 
                + "<span id='math'></span>","text/html","utf-8", "");


        return true;
    }

在Android 4.4模拟器上运行时,我遇到以下错误。

V/WebViewChromium(1342): Binding Chromium to the background looper Looper{b1da1340}
 I/chromium(1342): [INFO:library_loader_hooks.cc(112)] Chromium logging enabled: level = 0, default verbosity = 0
 I/BrowserProcessMain(1342): Initializing chromium process, renderers=0
 W/chromium(1342): [WARNING:proxy_service.cc(888)] PAC support disabled because there is no system implementation
 E/chromium(1342): [ERROR:gl_surface_egl.cc(153)] No suitable EGL configs found.
 E/chromium(1342): [ERROR:gl_surface_egl.cc(620)] GLSurfaceEGL::InitializeOneOff failed.
 E/chromium(1342): [ERROR:gl_surface_egl.cc(153)] No suitable EGL configs found.
 E/chromium(1342): [ERROR:gl_surface_egl.cc(620)] GLSurfaceEGL::InitializeOneOff failed.
 E/chromium(1342): [ERROR:gpu_info_collector.cc(86)] gfx::GLSurface::InitializeOneOff() failed

I/chromium(1101): [INFO:CONSOLE(1)] "Uncaught ReferenceError: MathJax is not defined", source: http://bar/ (1)

更新:在合并ksasq的建议后,这是我的新代码,但它仍然无效。

webview.loadDataWithBaseURL("http://bar", "<script type='text/x-mathjax-config'>"
      + "function setupMathJax() {"
                + "MathJax.Hub.Config({ messageStyle: 'none', showMathMenu: false, jax: ['input/TeX','output/HTML-CSS'], "
                    +"extensions: ['tex2jax.js'],'HTML-CSS': { scale: 100 },"            
                    +"TeX: { extensions: ['AMSmath.js','AMSsymbols.js','noErrors.js','noUndefined.js'] } });" 
     + "}"
                + "</script>"
                + "<script type='text/javascript' src='file:///android_asset/MathJax/MathJax.js'></script>" 
     + "<body onload='setupMathJax()'>"
                + "<span id='math'></span>" 
     + "</body>"
                ,"text/html","utf-8", "");

4 个答案:

答案 0 :(得分:4)

您必须将所有行loadUrl(...)替换为evaluateJavascript(...)。但这仅适用于KitKat(API 19或更高版本),因此您需要先进行SDK版本检查:

if (android.os.Build.VERSION.SDK_INT < 19) {
    mWebView.loadUrl("javascript:MathJax.Hub.Queue(['Typeset',MathJax.Hub]);");
} else {
    mWebView.evaluateJavascript("javascript:MathJax.Hub.Queue(['Typeset',MathJax.Hub]);",null);
}

答案 1 :(得分:1)

if (android.os.Build.VERSION.SDK_INT < 19) {
                    v.loadUrl(url);
                } else {
                    v.evaluateJavascript(url,null);
                }

答案 2 :(得分:0)

在加载MathJax标记之前,似乎不会设置<script>变量。您应该将其移动到onload侦听器中。类似的东西:

<script type='text/x-mathjax-config'>
   function setupMathJax() {
     MathJax.Hub.Config(...);
   }
</script>
<script type='text/javascript' src='file:///android_asset/MathJax/MathJax.js'></script>
<body onload='setupMathJax()'>
<span id='math'></span>
</body>

答案 3 :(得分:0)

在第二个<script>加载function setupMathJax()文件之前,包含<script>的第一个MathJax可能会被执行,导致MathJax is not defnied的异常。尝试颠倒这些标签的顺序。