title: how to encode href attribute in HTML
body: <p>What should be done against contents of href attribute: HTML or URL encoding?</p>
  
  <pre><code>&lt;a href="???"&gt;link text&lt;/a&gt;
  </code></pre>
  
  <p>On the one hand, since href attribute contains URL I should use URL encoding. 
  On the other hand, I'm inserting this URL into HTML, so it must be HTML encoded.</p>
  
  <p>Please help me to overcome this contradiction.</p>
  
  <p>Thanks.</p>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>Here's the contradiction. Suppose there might be the '&lt;' and '>' characters in the URL. URL encoding won't escape them, so there will be reserved HTML characters inside the href attribute, which violates the standard. HTML encoding will escape '&lt;' and '>' characters and HTML will be valid, but after that there will be unexpected '&amp;' characters in the URL (this is reserved character for URL, it's used as a delimiter of query string parameters).</p>
  
  <p><a href="http://en.wikipedia.org/wiki/Percent-encoding" rel="noreferrer">Reserved URL characters</a> forms a superset of <a href="http://www.w3schools.com/tags/ref_entities.asp" rel="noreferrer">reserved HTML characters</a> except for the '&lt;' and '>' that are reserved for HTML but not for URL.</p>
  
  <hr>
  
  <p>EDIT 2:</p>
  
  <p>I was wrong about '&lt;' and '>' characters, they are actually percent escaped by URL encoding. If so, URL encoding is sufficient in this case, isn't it?</p>