title: javascript html5 history, variable initialization and popState
body: <p><strong>main question</strong></p>
  
  <p>Is there a javascript way to identify if we are accessing a page for the first time or it is a cause of a back?</p>
  
  <p><strong>My problem</strong></p>
  
  <p>I'm implementing html5 navigation in my ajax driven webpage.</p>
  
  <p>On the main script, I initialize a variable with some values.</p>
  
  <pre><code>&lt;script&gt;
      var awnsers=[];
      process(awnsers);
  &lt;script&gt;
  </code></pre>
  
  <p>Process(awnsers) will update the view according to the given awnsers, using ajax.</p>
  
  <p>In the funciton that calls ajax, and replaces the view, I store the history</p>
  
  <pre><code>history.pushState(state, "", "");
  </code></pre>
  
  <p>I defined the popstate also, where I restore the view according to the back. Moreover, I modify the global variable awnsers for the old value.</p>
  
  <pre><code>function popState(event) {
  if (event.state) { 
      state = event.state;
      awnsers=state.awnsers;
      updateView(state.view);
  }
  }
  </code></pre>
  
  <p>Navigation (back and forth) goes corectly except when I go to an external page, and press back (arrving to my page again).
  As we are accessing the page, first, the main script is called,the valiable awnsers is updated, and the ajax starts. Meanwile, the pop state event is called, and updates the view. After that the main ajax ends, and updates the view according to empty values.</p>
  
  <p>So I need the code:</p>
  
  <pre><code>&lt;script&gt;
      var awnsers=[];
      process(awnsers);
  &lt;script&gt;
  </code></pre>
  
  <p>only be called when the user enters the page but NOT when it is a back. Any way to do this?</p>
  
  <p>THanks!</p>
  
  <p><strong>Possible solution</strong>
  After the first awnser I have thought of a possible solution. Tested and works, whoever, I don't know if there is any cleaner solution. I add the changes that I've done.</p>
  
  <p>First I add:</p>
  
  <pre><code>$(function() {
  justLoaded=true;
  });
  </code></pre>
  
  <p>then I modify the popState function, so that is in charge to initialize the variables</p>
  
  <pre><code>function popState(event) {
  if (event.state) { 
      state = event.state;
      awnsers=state.awnsers;
      updateView(state.view);
  } else if(justLoaded){
         awnsers=[];
         process(awnsers);
      }
      justLoaded=false;
  }
  </code></pre>
  
  <p>Thats all.</p>