Android - Loading an external .js file into a webview then accessing it's functions?

前端 未结 1 1501
不思量自难忘°
不思量自难忘° 2021-02-03 15:49

I have a JS file that has functions to search a document for substrings.

I want to access functions inside this file by passing parameters to it (the search keyword).

相关标签:
1条回答
  • 2021-02-03 16:49

    You can try this.

    webview.getSettings().setJavaScriptEnabled(true);  
    webview.setWebViewClient(new WebViewClient() {  
      @Override  
      public void onPageFinished(WebView view, String url){
        webview.loadUrl("javascript:(function() { " +  
        "var script=document.createElement('script');" +
        "script.type='text/javascript';script.src=" + jsFileURL + ";" +
        "script.onload=function("+queryString+"){//it can be your search function};"
        "document.getElementsByTagName('head').item(0).appendChild(script);"+  
        "})()");  
      }  
    });  
    webview.loadUrl("http://SOMEURL");
    0 讨论(0)
提交回复
热议问题