title: Encoding URL components with an ampersand &
body: <p>Seems like a very simple question here:</p>
  
  <p>I've got a program where someone is entering a string <code>M&amp;S</code> in a form and running a query.  I understand the <code>&amp;</code> is a reserved character and therefore must be encoded.  The problem is it seems to be requiring encoding twice in some contexts.  </p>
  
  <p>If the URL is used in a javascript <code>onClick</code> event, normal URL encoding seems to work fine (here the operator can click on a column header to sort):</p>
  
  <pre><code>&lt;td onClick="AJAX_Get('http://10.0.0.195/program.exe?Qry147=M%26S&amp;sortmethod1=161')"&gt;
  </code></pre>
  
  <p>However, if the URL is used in an anchor (although the anchor actually uses AJAX), it seems to need encoding twice:</p>
  
  <pre><code>&lt;a href="javascript:AJAX_Get('http://10.0.0.195/program.exe?Qry147=M%2526S&amp;sortmethod1=147')" title='Refresh'&gt;Refresh&lt;/a&gt;
  </code></pre>
  
  <p>Both of the examples above work fine.  However they are hand-generated test cases.  Unfortunately, in the application, when I'm actually generating the URL, I don't know how it's going to be used.</p>
  
  <p>If I encode the URL parameter once (<code>M%26S</code>), it works fine in <code>onClick</code>.  But used this way in the anchor, the server sees the URL as <code>...Qry147=M&amp;S&amp;sortmethod1=147...</code> - so it must have been unencoded before being given back to the server.</p>
  
  <p>If I encode it twice (<code>M%2526S</code>), the anchor works, but for the <code>onClick</code>, the server sees <code>...Qry147=M%2526S...</code>.</p>
  
  <p>I get the feeling I'm missing something here.  Is there a way to make this work the same in both cases?</p>