title: Android webview, loading javascript file in assets folder
body: <p>I've seen this question has been asked a lot of times, but still can't manage to get my code working.</p>
  
  <p>I want my <em>webview</em> to load some URL (say www.google.com) and then apply some javascript stored in <code>assets/jstest.js</code>, which contains the following:</p>
  
  <pre><code>function test(){
  document.bgColor="#00FF00"; //turns to green the background color
  }
  </code></pre>
  
  <p>And here's where I try to load the JS:</p>
  
  <pre><code>@Override  
  public void onPageFinished(WebView view, String url){
      view.loadUrl("javascript:(function() { "
                  + " document.bgColor='#FF0000';" //turns to red the background color
                  + " var script=document.createElement('script'); "
                  + " script.setAttribute('type','text/javascript'); "
                  + " script.setAttribute('src', 'file:///android_asset/jstest.js'); "
                  + " script.onload = function(){ "
                  + "     test(); "
                  + " }; "
                  + " document.getElementsByTagName('head')[0].appendChild(script); "
                  + "})()"); 
  } 
  </code></pre>
  
  <p>I know the javascript here works because the background color actually turns to red, but for some reason it won't load <code>jstest.js</code>. I think the problem might be in file path (I'm certain every other line of the javascript code is correct), but it looks correct to me. And the file is in the right folder.</p>
  
  <p>What am I missing?</p>
  
  <p><strong>EDIT</strong>:</p>
  
  <p>Since <code>WebResourceResponse</code> class is available only with API Level 11, here's what I've figured out in the end.</p>
  
  <pre><code>public void onPageFinished(WebView view, String url){
          String jscontent = "";
          try{
              InputStream is = am.open("jstest.js"); //am = Activity.getAssets()
              InputStreamReader isr = new InputStreamReader(is);
              BufferedReader br = new BufferedReader(isr);
  
              String line;
              while (( line = br.readLine()) != null) {
                  jscontent += line;
              }
              is.close(); 
          }
          catch(Exception e){}
          view.loadUrl("javascript:(" + jscontent + ")()"); 
      } 
  </code></pre>
  
  <p>with the <code>jstest.js</code> simply containing:</p>
  
  <pre><code>function() {
      document.bgColor="#00FF00";
  }
  </code></pre>