title: Use javascript in an android webview
body: <p>I am using a webview that reads from a local HTML in my project. I am using some javascript on the HTML after if has finished loading the html file. This is my set-up:</p>
  
  <pre><code>        final WebView webview = (WebView)rootView.findViewById(R.id.webView);
          webview.getSettings().setJavaScriptEnabled(true);
          final String letter = "hello";
  
          webview.setWebViewClient(new WebViewClient() {
              @Override
              public void onPageFinished(WebView view, String url)
              {
                  webview.loadUrl("javascript:(function() { " +
                          "var text = 'HELLO';"+
                          "document.body.innerHTML = document.body.innerHTML.replace(/"+letter+"/g, '&lt;font color=\"#FF0000\"&gt;text&lt;/font&gt;');"+
                          "})()");
              }
          });
  
          webview.loadUrl("file:///android_asset/2.html");
  </code></pre>
  
  <p>Basically what this does now is to change the word hello into a red word text. But what i want it to do is to change the word into HELLO as i declared in the javascript. So my question is how to incorporated variables into javascript code this way. Btw i know i could delcare var text outside where i declare String letter. But this is just a small example of what i try to do. </p>
  
  <p>I would be very grateful if someone could explain how to use forloops inside the webview.LoadUrl(javascript code below:</p>
  
  <pre><code>var color = "#FF0000";
  var text = "fem";
  for (i = 0; i &lt; 5; i++) {
      text += "The number is " + i + "&lt;br&gt;";
  }
     document.body.innerHTML = document.body.innerHTML.replace(/with/g,     '&lt;font color="'+color+'"&gt;'+text+'&lt;/font&gt;');
  </code></pre>
  
  <p>How do i put it inside webview.loadURL. Please dont hesitate to ask if something was unclear. </p>