title: WebView jump to anchor using loadDataWithBaseUrl
body: <p>My Android app uses a WebView to display a bunch of HTML code that I generate 'on the fly'. The HTML code is loaded using the following code:</p>
  
  <pre class="lang-java prettyprint-override"><code>    StringBuilder builder = new StringBuilder();
  
      // HTML
      builder.append("&lt;html&gt;&lt;head&gt;&lt;link rel=\"stylesheet\" href=\"file:///android_asset/style.css\" type=\"text/css\"&gt;");
      builder.append("&lt;/link&gt;&lt;/head&gt;&lt;body&gt;");
      builder.append(getThreadBody());
      builder.append("&lt;/body&gt;&lt;/html&gt;");
  
      webview.loadDataWithBaseURL("file:///android_asset/", builder.toString(), "text/html", "utf-8", null);
  </code></pre>
  
  <p>This all works really nice. Note that I'm not loading an actual HTML file, I'm merely creating a string that represents some (hopefully valid) HTML and load it in the WebView. </p>
  
  <p>Anyway, the HTML I generate (the part in the 'getThreadBody' method) contains named anchors, for example like this;</p>
  
  
  
  <pre><code>&lt;div&gt;
      &lt;a name="949823"&gt;Anchor 1&lt;/a&gt;
      &lt;div&gt;All kinds of stuff&lt;/div&gt;
  
      &lt;a name="895984"&gt;Anchor 2&lt;/a&gt;
      &lt;div&gt;...&lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Now in some cases I want the WebView to navigate to one of those anchors as soon as I load the HTML. As far as I understand the WebView supports navigating (scrolling in this case) to named anchors, but the catch is that nobody is clicking any hyperlinks to those anchors, I want the WebView to scroll when it is loaded (it is no problem if there is a short delay between loading the HTML and scrolling, my point is that it should not require user interaction).</p>
  
  <p>I believe the behavior can be achieved by using the WebView's loadUrl method and supplying a URL with the anchor in place, eg</p>
  
  <pre class="lang-java prettyprint-override"><code>webview.loadUrl("file:///android_asset/page.html#895984", ...)
  </code></pre>
  
  <p>However, since I am not saving the HTML to any file I cannot use this method... Of course saving the HTML to a temporary file may be a solution but I'd like to keep that as a last resort, there must be a simpler way? </p>
  
  <p>How can I achieve this? Thanks!</p>
  
  <hr>
  
  <p><strong>Resolved</strong>
  Here's the code that works. I found a short delay was required to let the page load before executing the javascript otherwise it was kind of 50/50 whether it worked or not...</p>
  
  <pre><code>    StringBuilder builder = new StringBuilder();
  
      // HTML
      builder.append("&lt;html&gt;&lt;head&gt;&lt;link rel=\"stylesheet\" href=\"file:///android_asset/style.css\" type=\"text/css\"&gt;");
      builder.append("&lt;/link&gt;");
      builder.append("&lt;script&gt;");
      builder.append("function scrollAnchor(id) {");
      builder.append("window.location.hash = id;}");
      builder.append("&lt;/script&gt;");
      builder.append("&lt;/head&gt;&lt;body&gt;");
      builder.append(getThreadBody());
      builder.append("&lt;/body&gt;&lt;/html&gt;");
  
      webContents.loadDataWithBaseURL("file:///android_asset/", builder.toString(), "text/html", "utf-8", null);
  
      Timer timer = new Timer();
      timer.schedule(new TimerTask() {
          @Override
          public void run() {
              String id = "895884";
              webContents.loadUrl("javascript:scrollAnchor(" + id + ");");
          }
          }
      }, 250);
  </code></pre>