Android的 - 加载外部js文件到一个web视图,然后访问它的功能呢? [英] Android - Loading an external .js file into a webview then accessing it's functions?

查看:153
本文介绍了Android的 - 加载外部js文件到一个web视图,然后访问它的功能呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有功能搜索子文档中的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");

这篇关于Android的 - 加载外部js文件到一个web视图,然后访问它的功能呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆