简体   繁体   中英

uncaught referenceerror function is not defined javascript android

I try to load html page to custom webView also i need to load 4 javascript files to the webView, when i put the javascript reference in the head tag in html directly, they worked well, but when i load them at run time from my android function, they loaded correctly, but dont work and catch "uncaught referenceerror function is not defined" exception.

here is what i do in two status:

status 1: load reference directly:

this works perfectly.....

<html>

<head>
<head>

<script type="text/javascript" src="file:///android_asset/selection/android.selection.js"></script>
<script type="text/javascript" src="file:///android_asset/selection/jquery.js"></script>
<script type="text/javascript" src="file:///android_asset/selection/rangy-core.js"></script>
<script type="text/javascript" src="file:///android_asset/selection/rangy-serializer.js"></script>      
</head>
<body>
......
</body>
</html>

status 2: load from android application in webview client,,, onPageFinished method i put this code:

String str =  "javascript: (function() { "

            + " var rangycore=document.createElement('script');"
            + " rangycore.type='text/javascript';"
            + " rangycore.src='file:///android_asset/selection/rangy-core.js';"
    +"document.getElementsByTagName('head').item(0).appendChild(rangycore);"

            + " var rangyserializer=document.createElement('script');"
            + " rangyserializer.type='text/javascript';"
            + " rangyserializer.src='file:///android_asset/selection/rangy-serializer.js';"
            + " document.getElementsByTagName('head').item(0).appendChild(rangyserializer);"

            +  " var select=document.createElement('script');"
            + " select.type='text/javascript';"
            + " select.src='file:///android_asset/selection/android.selection.js';"
            + " document.getElementsByTagName('head').item(0).appendChild(select);"

            + " var jquery=document.createElement('script');"
            + " jquery.type='text/javascript';"
            + " jquery.src='file:///android_asset/selection/jquery.js';"
            + " document.getElementsByTagName('head').item(0).appendChild(jquery);})()";


    BTWebView.this.loadUrl(str);

this print console message "uncaught referenceerror: rangy is not defined" although the head tag appended correctly with the scripts string.

how i can solve this problem and thank you....

A much simpler and elegant way to load JavaScript during runtime ist yepnope , a small 1.7 KB library.

yepnope({
  load: [
    "file:///android_asset/selection/android.selection.js",
    "file:///android_asset/selection/jquery.js",
    "file:///android_asset/selection/rangy-core.js",
    "file:///android_asset/selection/rangy-serializer.js"
  ],
  complete: function () {
    alert('loaded successfully');
  }
});

You will have less code and therefor maybe less bugs in your concatenated string.

And the available callbacks might help finding errors or to add useful features.

Your are loading the files in a different order in your two examples!

The order of loading script files is important. I guess you have dependencies, which could not be resolved.

Try it:- If the app targets LOLLIPOP or above the default web view behaviour is to completely deny mixed content. As mobile connect requires the use of HTTP during header enrichment we need a more relaxed security setting. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { webView.getSettings().setMixedContentMode(WebSettings.MIXED_‌CONTENT_COMPATIBILIT‌Y_MODE); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM