title: Why does Android WebView reset JavaScript context on second page load?
body: <p>I am using an Android WebView with local data by first setting up a JavaScript environment and then loading web content that relies on the existence of the JavaScript environment:</p>
  
  <p><strong>test.js:</strong></p>
  
  <pre><code>alert("test.js: type of window.myVar is: " + (typeof window.myVar));
  window.myVar = {};
  </code></pre>
  
  <p><strong>test.html:</strong></p>
  
  <pre><code>&lt;!DOCTYPE&gt;
  &lt;html&gt;&lt;head&gt;
  &lt;script&gt;
  alert("test.html: type of window.myVar is: " + (typeof window.myVar));
  &lt;/script&gt;
  &lt;/head&gt;&lt;body&gt;&lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p><strong>loading procedure in Java:</strong></p>
  
  <pre><code>private void loadData() {
      _webView.loadUrl("javascript:" + testJSContent);
      _webView.loadDataWithBaseURL(null, testHTMLContent, "text/html", "utf-8", null);
  }
  </code></pre>
  
  <p>The first time <em>loadData()</em> is triggered, <em>window.myVar</em> is available from the loaded HTML page, but <em>undefined</em> on any subsequent tries. Here is the alert output on three invokations:</p>
  
  <p>-> loadData() called<br />
  <em>"test.js: type of window.myVar is: undefined"</em><br />
  <em>"test.html: type of window.myVar is: object"</em><br />
  -> loadData() called<br />
  <em>"test.js: type of window.myVar is: object"</em><br />
  <em>"test.html: type of window.myVar is: undefined"</em><br />
  -> loadData() called<br />
  <em>"test.js: type of window.myVar is: undefined"</em><br />
  <em>"test.html: type of window.myVar is: undefined"</em><br /></p>
  
  <p><br />
  Any ideas why the JavaScript object created by injection is only persistent until the second page load?</p>
  
  <p>Thanks for any help!</p>