E/Web Console(8272): Uncaught ReferenceError: functionName is not defined:1 while loading webviews in a View Pager

前端 未结 3 1236
星月不相逢
星月不相逢 2021-01-21 14:47

I am trying to load webviews in a view pager.

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanc         


        
相关标签:
3条回答
  • 2021-01-21 14:59

    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!

    0 讨论(0)
  • 2021-01-21 15:05

    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;
        }
    
    0 讨论(0)
  • 2021-01-21 15:22

    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
     }
    
    0 讨论(0)
提交回复
热议问题