title: Query String parsing: Handling both & and ;
body: <p>I've found plenty of articles on StackOverflow that mention fast ways to parse query string's in Javascript, but what I haven't found is how to handle both <code>&amp;</code> and <code>;</code> as a delimiter.</p>
  
  <p>Now, I assume, only one of those gets set as the delimiter for the query string. Take for instance, the following query string:</p>
  
  <pre><code>index.php?Param1=value&amp;Param2=value;Param3=value
  </code></pre>
  
  <p>Which is the delimiter?</p>
  
  <ul>
  <li>Is it <code>&amp;</code>, or is it <code>;</code>?</li>
  <li>Is it the first recognized delimiting character, or is it <code>&amp;</code> takes precedence over <code>;</code>?</li>
  <li>Are both treated as delimiters, and all three parameters from the example parsed?</li>
  </ul>
  
  <p>In Javascript, I have historically been using the following function:</p>
  
  <pre><code>/**
   * Gets the value of a parameter from a URI.
   * @param href The full URI to search. Must at least start with a ? query string delimiter.
   * @param key  The key of the param for which to retrieve the value.
   * @param def  A default value to return in the case that the key cannot be found in the query string.
   * @return The value of the param if the key is found, otherwise returns def.
   */
  function getURIParam(href, key, def) {
      if (arguments.length == 2) def = null;
  
      var qs = href.substring(href.indexOf('?') + 1);
      var s = qs.split('&amp;');
      for (var k in s) {
          var s2 = s[k].split('=');
          if (s2[0] == key)
              return decodeURIComponent(s2[1]);
      }
  
      return def;
  }
  </code></pre>
  
  <p>That just splits out each key/value pair based on <code>&amp;</code>. It works great, so long as the delimiter is always <code>&amp;</code>. As we know, however, that is not always the case, nor should be enforced, as the RFC allows for <code>;</code> as well.</p>
  
  <p>So, in order to handle both <code>&amp;</code> and <code>;</code> as a delimiter, should I first search for an <code>indexOf("&amp;")</code> and if no occurrence is found, set the delimiter to <code>;</code>?</p>
  
  <p>What is the proper way of parsing a URL based on the rule that the delimiter can be either <code>&amp;</code> or <code>;</code>?</p>
  
  <p>Here is the <a href="http://www.w3.org/TR/1999/REC-html401-19991224/appendix/notes.html#h-B.2.2" rel="nofollow">W3C Recommendation</a> on server being able to handle both <code>&amp;</code> and <code>;</code>.</p>
  
  <blockquote>
    <p>We recommend that HTTP server implementors, and in particular, CGI
    implementors support the use of ";" in place of "&amp;" to save authors
    the trouble of escaping "&amp;" characters in this manner.</p>
  </blockquote>
  
  <p>Here is <a href="http://www.ietf.org/rfc/rfc1738.txt" rel="nofollow">RFC 1738</a> which defines the URI.</p>