5

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. But to add js file in html page, I need to define it inside the html file like:

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

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. This sounds like a simple task, but I am unable to find any solution for this. Any ideas?

Community
  • 1
  • 1
berserk
  • 2,690
  • 3
  • 32
  • 63

1 Answers1

4

You will want to use loadDataWithBaseURL for your purpose.

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

     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')");
Max Worg
  • 2,932
  • 2
  • 20
  • 35
  • Can you please elaborate your answer? I already read docs on this function. – berserk Jan 22 '15 at 22:17
  • Here is a complete example that may help you: http://stackoverflow.com/questions/9414312/android-webview-javascript-from-assets – Max Worg Jan 22 '15 at 22:19
  • Ok I used loadDataWithBaseURL("file:///android_asset/", readHtml(filePath), "text/html", "UTF-8", ""); But it is still not loading my js files. And what about the step 3? Do I need to manually add that script tag inside my html file? – berserk Jan 22 '15 at 23:29
  • Yes you will want to load the script tags into your html file and make sure that you get the path to your resources correct. , etc – Max Worg Jan 22 '15 at 23:33
  • So you mean to say I must manually append the script tag in the html content right? – berserk Jan 22 '15 at 23:34
  • That is correct. You will manually load those script tags in the html content so that when the html is loaded it will then direct your Android app to look locally for your javascript files. – Max Worg Jan 22 '15 at 23:36
  • Well if this was the case, loadDataWithBaseUrl() is not the solution for my query. I could have just used loadData() with the html content having script tags inside it(). – berserk Jan 22 '15 at 23:39
  • My actual question was : "How to load script without actually appending the script tag inside html?". – berserk Jan 22 '15 at 23:43
  • I updated my answer. I don't see that you have a loadScript() function actually loaded in your HTML so your initial call to loadScript will fail with a nullpointer exception. – Max Worg Jan 23 '15 at 00:03
  • So basically it will add tags to the html content. Well, thanks for your answer. +1 :) – berserk Jan 23 '15 at 00:22