title: Problems with Page Cache in iOS 5 Safari when navigating back / unload event not fired
body: <p><strong>tl;dr</strong> - Safari on iOS 5 is caching so hard, it is breaking my site.</p>
  
  <p>I am struggling with the way the Safari browser in iOS 5 deals with back forward cache, which they call "Page Cache".  The way it is described <a href="http://www.webkit.org/blog/427/webkit-page-cache-i-the-basics/" rel="noreferrer">here</a> explains the behavior very well. </p>
  
  <blockquote>
    <p>Quite simply, the Page Cache makes it so when you leave a page we “pause” it and when you come back we press “play.”</p>
  </blockquote>
  
  <p>This is causing problems throughout my site.  When using the back button, most other browsers will show you the page in the state it was loaded.  Not Safari on iOS 5, it shows you the page as you last left it.  A simple example would be the disabling of a submit button.  If I use Javascript to disable a submit button, then submit a form, when you click back the submit button will still be disabled.  This has been an issue in other browsers, including the desktop version of Safari, but it is solved by setting the onload event handler to a blank function.  I believe this tells the browser to invalidate the cache because something important could have happened in that function.  This hack does not seem to work for Safari on iOS 5.</p>
  
  <p>Below is the issue boiled down to the bare essentials.  When you load test.html you will see the text "Original Text".  When you click the link, that text will change to "Changed text - forwarding to next page", then in 3 seconds you will be forwarded to test2.html.  All is good up to this point in all browsers.  In all other browsers, when you click the back button, the text you will see is "Original Text", but on Safari for iOS 5 you will see "Changed text - forwarding to next page".</p>
  
  <p>Any suggestions on how to deal with this?</p>
  
  <p><strong>This is a simple example</strong></p>
  
  <p>test.html</p>
  
  <pre><code>&lt;script&gt;
  function changeText() {
      el = document.getElementById("text");
      el.innerHTML = "Changed text - forwarding to next page";
      setTimeout("forward()",3000);       
  }
  function forward() {
      document.location.href = "test2.html";
  }
  &lt;/script&gt;
  &lt;div id="text"&gt;Original Text&lt;/div&gt;
  &lt;a href="Javascript:changeText()"&gt;Click Here&lt;/a&gt;
  &lt;script&gt;
  window.onunload = function(){};
  &lt;/script&gt;
  </code></pre>
  
  <p>test2.html</p>
  
  <pre><code>&lt;div&gt;Click back button&lt;/div&gt;
  </code></pre>
  
  <p><strong>This is a second example using a form post.  This is a simple example of how my app is working.  When you navigate back to formtest2.asp, you should see the posted form value and the div text should be original.</strong></p>
  
  <p>formtest.asp</p>
  
  <pre><code>&lt;form method="post" action="formtest2.asp"&gt;
      Test: &lt;input type="text" name="test"/&gt;
      &lt;input type="submit" value="Submit"/&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>formtest2.asp</p>
  
  <pre><code>&lt;script&gt;
  function changeText() {
      el = document.getElementById("text");
      el.innerHTML = "Changed text - forwarding to next page";
      setTimeout("forward()",3000);       
  }
  function forward() {
      document.location.href = "test2.html";
  }
  &lt;/script&gt;
  
  &lt;%
  Dim test
  test = Request("test")
  Response.Write("Test value: " &amp; test &amp; "&lt;br /&gt;")
  %&gt;
  
  &lt;div id="text"&gt;Original Text&lt;/div&gt;
  &lt;a href="Javascript:changeText()"&gt;Click Here&lt;/a&gt;
  &lt;script&gt;
  window.onunload = function(){};
  &lt;/script&gt;
  </code></pre>
  
  <p>test2.html</p>
  
  <pre><code>&lt;div&gt;Click back button&lt;/div&gt;
  </code></pre>