[英]Loading a js file from assets into webview after loading html
I searched a lot, but unable to find any solution. 我搜索了很多,但无法找到任何解决方案。 I am basically making an epub reader in webview with some features like highlighting text etc. I made some javascript files for this.
我基本上在webview中使用突出显示文本等功能制作一个epub阅读器。我为此制作了一些javascript文件。 But to add js file in html page, I need to define it inside the html file like:
但是要在html页面中添加js文件,我需要在html文件中定义它,如:
<script src='file:///android_asset/jquery.js'></script>
But as the html can be any file, I must load the file using some other method. 但由于html可以是任何文件,我必须使用其他方法加载文件。 I tried using
我试过用
view.loadUrl("javascript:loadScript('file:///android_asset/jquery.js','callback')");
but it is not loading my js (I call some of its functions, but they didn't worked). 但它没有加载我的js(我称它的一些功能,但它们没有工作)。
I also tried this solution, but not worked. 我也试过这个解决方案,但没有奏效。 Basically, I just need to load my js file on the webview so that I can use its functions later.
基本上,我只需要在webview上加载我的js文件,以便以后可以使用它的功能。 This sounds like a simple task, but I am unable to find any solution for this.
这听起来像一个简单的任务,但我无法找到任何解决方案。 Any ideas?
有任何想法吗?
You will want to use loadDataWithBaseURL for your purpose. 您将需要使用loadDataWithBaseURL来达到目的。
UPDATE: 更新:
I don't see an actual method loadScript() that is loaded into the page anywhere so you can try to create the loadScript method yourself before you call to javascript:loadScript(). 我没有看到在任何地方加载到页面中的实际方法loadScript(),因此您可以在调用javascript:loadScript()之前尝试自己创建loadScript方法。
view.loadUrl("javascript:function loadScript(url, callback)" +
"{" +
" var head = document.getElementsByTagName('head')[0];" +
" var script = document.createElement('script');" +
" script.type = 'text/javascript';" +
" script.src = url;" +
" script.onreadystatechange = callback;" +
" script.onload = callback;" +
" head.appendChild(script);" +
"}");
view.loadUrl("javascript:loadScript('file:///android_asset/jquery.js','callback')");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.