title: Prevent window.onhashchange from executing when hash is set via JavaScript
body: <p>I use the <code>window.onhashchange</code> function to execute code when the User changes the hash of the page:</p>
  
  <pre><code>window.onhashchange = function() { /* do something */ };
  </code></pre>
  
  <p>In some functions I also set the hash via JavaScript:</p>
  
  <pre><code>window.location.hash = "#abc";
  </code></pre>
  
  <p>I want to prevent the <code>onhashchange</code> event from firing when I set the hash via JavaScript.</p>
  
  <p>What I have tried so far:</p>
  
  <pre><code>var currently_setting_hash = false;
  
  window.onhashchange = function() {
    if (currently_setting_hash)
      return;
   //...
  }
  
  currently_setting_hash = true;
  window.location.hash = "#abc";
  currently_setting_hash = false;
  </code></pre>
  
  <p>That didn't work because the event is fired with a delay, so the code will first set the hash, then set <code>currently_setting_hash</code> to false and then execute the <code>onhashchange</code> event.</p>
  
  <p>Any ideas how this could be accomplished? Or maybe is there a way to detect if the hash was set by the user or via JavaScript?</p>