title: How to prevent onload from running always?
body: <p>I have a function I want to run when the homepage of a website is first loaded. However, I don't want that function to run if, instead of getting to the homepage by accessing it directly, I get there because I clicked on a link on the site that directs me there.<br>
  For this, I direct every link that gets you to the homepage to <code>home.html#loaded</code> instead of <code>home.html</code>. I also created the function below to be the one <code>onload</code> executes:</p>
  
  <pre><code>function onloadPreventer(fun) {
      if (window.location.hash != "#loaded") {
          //alert(window.location.hash);
          window.location = window.location + "#loaded";
          //alert(window.location.hash);
          fun;
      };
  };
  </code></pre>
  
  <p>This function should only execute the function <code>fun</code> if there is no <code>#loaded</code> after <code>home.html</code>. However, it always executes <code>fun</code>.<br>
  However, the weird part is that it does not add <code>#loaded</code> once again:</p>
  
  <p>When I first go to <code>home.html</code> (without hash) -- and if I comment out the <code>alert</code> statements -- it first gives me nothing, then gives me <code>#loaded</code> and then runs <code>fun</code>. All as it should.<br>
  If I go to <code>home</code> from another page, I'm already first loading <code>home.html#loaded</code>. The page alerts nothing, <code>onloadPreventer</code> does not add another <code>#loaded</code> at the end of the current location, <strong>but <code>fun</code> runs anyway</strong>.</p>
  
  <p>The above function is called from inside the <code>&lt;body&gt;</code> tag, through an <code>onload</code> event:  </p>
  
  <pre><code>&lt;body onload="onloadPreventer(anotherFunction(arg))"&gt;
  </code></pre>
  
  <p>Unlike what the comments below suggest, <code>fun</code> <em>does</em> run. My problem is that even if the page has the hash <code>#loaded</code>, the rest of the code inside the <code>if</code> statement does not run, but <code>fun</code> still does.</p>
  
  <p>What's wrong with this code?<br>
  And is there another way (possibly simpler) to accomplish what I am trying to accomplish?</p>