简体   繁体   English

将HTML文件加载到包含外部js脚本的WebView中

[英]Load HTML file into WebView that contain external js script

I want to load a html code into a webview. 我想将html代码加载到网络视图中。 But the html code has several other external resources. 但是html代码还有其他一些外部资源。 for example in the html code I have : 例如在html代码中,我有:

<script type="text/javascript" src="//cdn3.example.com/example.js"></script>

but it seems that 'example.js' is not executed when rendering the webview. 但似乎在渲染Webview时未执行“ example.js”。

Is it possible to achieve that ? 有可能实现这一目标吗? Thank you 谢谢

I have similar issue, webview loaded from file:// and won't load external resources... cors set to * on the server side too. 我有类似的问题,webview从file://加载,并且不会加载外部资源...在服务器端也将cors设置为*。

So digging into it further, seems my problem was I used custom ssl certs on server side and android couldn't verify the server authenticity, and so requests were being cancelled. 因此,进一步研究它,似乎我的问题是我在服务器端使用了自定义ssl证书,而android无法验证服务器的真实性,因此请求被取消。 (adding intermediate certs to app would help) (向应用程序添加中间证书会有所帮助)

alternately You can override webclient's shouldInterceptRequest and fetch the external scripts yourself, returning the request from which webview will draw data (make sure you don't fetch during the shouldInterceptRequest call or it will be slooooooow sequentially fetching resources; instead, return a request's subclass and start fetching data async, so by time call to getData comes you already have what you need... search so for async shouldInterceptRequest for help with that). 或者,您可以覆盖webclient的shouldInterceptRequest并自己获取外部脚本,返回从中获取Webview数据的请求(请确保您在shouldInterceptRequest调用期间不获取数据,否则它将顺序地获取资源;相反,返回请求的子类并开始异步获取数据,因此在调用getData的时候,您已经有了所需的内容...搜索异步的shouldInterceptRequest以获得帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM