Uncaught ReferenceError while loading asset file on android 4.4

為{幸葍}努か 提交于 2019-11-28 05:32:38

问题


I am using following MathJax app code. http://cs.jsu.edu/wordpress/?p=498#comment-217

In following function i am trying to load file from asset directory.

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;
    }

When running on android 4.4 emulator, I am getting following errors.

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)

Update: After incorporating ksasq's suggestion , Here is my new code but it is still not working.

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", "");

回答1:


You have to replace all the lines loadUrl(...) with evaluateJavascript(...). But this is only for KitKat (API 19 or above), so you need to do an SDK version check first:

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);
}



回答2:


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



回答3:


It looks like the MathJax variable won't be set until you load the <script> tag. You should move this into an onload listener. Something like:

<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>



回答4:


Perhaps the first <script> containing your function setupMathJax() is getting executed before your second <script> has loaded your MathJax file, causing the exception where MathJax is not defnied. Try reversing the order of those tags.



来源:https://stackoverflow.com/questions/20582282/uncaught-referenceerror-while-loading-asset-file-on-android-4-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!