title: URL fragment identifier - simplify state handling (javascript)
body: <p>I have a web application which makes extensive use of the fragment identifier for handling "state".</p>
  
  <pre><code>examplesite.com/#$mode=direct$aa;map=t;time=2003;vid=4;vid=7
  </code></pre>
  
  <p><em>A number of questions:</em></p>
  
  <p><strong>1) What is a good way of assigning the various "location.hash name value-pairs" to variables to keep track of the state?</strong></p>
  
  <p>1a) Should I make an object that keeps track of the state in js or declare global variables for each name value-pair?</p>
  
  <p>1b) Are there any good jquery plugins to simplify this?</p>
  
  <p>1c) If I want to keep track of something called "color" - should it at all times be appended to the fragment (#) and what is the correct way of checking if it is defined; can the code below be improved?</p>
  
  <pre><code>var color;
  
  var hashString = location.hash;
  var nvPairs = hashString.split(";");
  var nvPair = new Array();
  
  for (i = 0; i &lt; nvPairs.length; i++)
  {
      var keyValuePair = nvPairs[i].split("=");
      nvPair[keyValuePair[0]] = keyValuePair[1];
  }
  
  if (nvPair['color']) color = nvPair['color'];       
  </code></pre>
  
  <p>1d) As some names are used twice ("vid" in the example above) - how could I easily store them is separate variables?  </p>
  
  <p><strong>2) There are 4 different "hashes" I want to pay extra attention to:</strong></p>
  
  <p>examplesite.com/ <strong>(no hash)</strong><br>
  examplesite.com/#example=5 <strong>(contains "example")</strong><br>
  examplesite.com/#time=2003;vid=4;vid=7;modified=5 <strong>(contains "modified")</strong><br>
  examplesite.com/#time=2003;vid=4;vid=7 <strong>(does not contain "modified" or "example")</strong></p>
  
  <p><strong>How would you write a control structure that extracts variables from the hash when the application loads and checks the above conditions?</strong></p>
  
  <p>3) How can the previous state/s be stored and how to trigger a change of state when the back-button is pressed?</p>