Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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?

Thanks!

like image 866
Sid Avatar asked Oct 28 '25 15:10

Sid


1 Answers

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");
like image 153
bhups Avatar answered Oct 30 '25 06:10

bhups