title: Detecting Back Button/Hash Change in URL
body: <p>I just set up my new homepage at <a href="http://ritter.vg" rel="noreferrer">http://ritter.vg</a>.  I'm using jQuery, but very minimally.<br>
  It loads all the pages using AJAX - I have it set up to allow bookmarking by detecting the hash in the URL.  </p>
  
  <pre><code> //general functions
   function getUrl(u) {
        return u + '.html';
   }
   function loadURL(u)    {
        $.get(getUrl(u), function(r){
                  $('#main').html(r);
             }
        );
   }
   //allows bookmarking
   var hash = new String(document.location).indexOf("#");
   if(hash &gt; 0)
   {
        page = new String(document.location).substring(hash + 1);
        if(page.length &gt; 1)
          loadURL(page);
        else
          loadURL('news');
   }
   else
        loadURL('news');
  </code></pre>
  
  <p>But I can't get the back and forward buttons to work.  </p>
  
  <p>Is there a way to detect when the back button has been pressed (or detect when the hash changes) without using a setInterval loop?  When I tried those with .2 and 1 second timeouts, it pegged my CPU.</p>