Android的 - 加载外部js文件到一个web视图,然后访问它的功能呢?它的、视图、加载、功能

2023-09-05 00:15:19 作者:梦之神

我有一个具有功能搜索子文档中的JS文件。

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

我知道我们可以用.loadUrl(JavaScript的:~~~~~),但我不清楚如何使用多种功能来做到这一点。

I know we can use .loadUrl("javascript:~~~~~) but I'm not clear on how to do it using multiple functions.

任何人谁可以在正确的方向指向我?

Anyone who can point me in the right direction?

谢谢!

推荐答案

您可以试试这个。

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