E/Web Console(8272): Uncaught ReferenceError: func

2020-04-17 01:48发布

I am trying to load webviews in a view pager.

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = null;      
    v = inflater.inflate(R.layout.webview_layout, container, false);
    myWebView = (WebView)v.findViewById(R.id.webview1);
    WebSettings webSettings = myWebView.getSettings();      
    webSettings.setJavaScriptEnabled(true);
    webSettings.setAppCacheEnabled(true);
    webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    myWebView.loadUrl("file:///android_asset/web/index.html");
    myWebView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            myWebView.loadUrl("javascript:testFunction()");
        }
    }
}

After loading the page, a javascript function is called in onPageFinished() While scrolling at normal speed the webpages are loading and the javascript is executed.

But while scrolling at high speed the following exception is occured.

> 09-06 14:29:06.750: E/Web Console(8272): Uncaught ReferenceError:
> testFunction is not defined:1

testFunction() is

function testFunction(){
    console.log("TestFuntion");     
}

Please help...

3条回答
该账号已被封号
2楼-- · 2020-04-17 02:20

I was getting Uncaught Reference Error: JavascriptInterfaceName is not defined on every second startup of my hybrid application on 4.3 and below, and so I did this thanks to @Abi in my WebChromeClient class:

@Override
@SuppressLint({"AddJavascriptInterface", "InflateParams"})
public boolean onConsoleMessage(@NonNull ConsoleMessage consoleMessage) {
    if("Uncaught ReferenceError: JavascriptInterfaceName is not defined".equals(consoleMessage.message())) {
        webView.addJavascriptInterface(new WebAppInterface(), "JavascriptInterfaceName ");
        webView.reload();
    }
    return super.onConsoleMessage(consoleMessage);
}

And it works! Thank you very much!

查看更多
女痞
3楼-- · 2020-04-17 02:32

try something like this and make changes as per your requirement

webview code

    web.getSettings().setJavaScriptEnabled(true);
    JavaScriptInterface JSInterface = new JavaScriptInterface(this);
    web.addJavascriptInterface(JSInterface, "JSInterface"); 
    setContentView(web);
    web.loadUrl("file:///android_asset/demo.html");

javascript interface

public class JavaScriptInterface {
        Context mContext;

        /** Instantiate the interface and set the context */
        JavaScriptInterface(Context c) {
            mContext = c;
        }

        public void showToast()
        {
            Toast.makeText(mContext, "Button Clicked", Toast.LENGTH_SHORT).show();
        }
    }

webpage code

<!DOCTYPE html>
<html>
  <head>
   <script>
     var i=0;
     function myFunction()
     {

       JSInterface.showToast();
     }
   </script>
 </head>

 <body>

    <button onclick="myFunction()">Try it</button>

    <p>By clicking the button above, a function will be called. The function will alert a message.</p>

 </body>
</html>

EDIT

function myFunction()
 {

   do here somthing
 }
查看更多
爷的心禁止访问
4楼-- · 2020-04-17 02:38

I had a fix on this

just set webChromeClient and catch the error and reload the page...

    myWebView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            isLoading = true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            myWebView.loadUrl("javascript:testFunction()");
        }
    });

    myWebView.setWebChromeClient(new WebChromeClient() {
        @Override
        public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
            MessageLevel level = consoleMessage.messageLevel();
            if(level.ordinal() == 3) { // Error ordinal
                if(loading) {
                    myWebView.stopLoading();
                    myWebView.loadUrl(AppConstants.ARTICLE_PAGE_URL);
                }
            }
        }
        return false;
    }
查看更多
登录 后发表回答