Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have a <a href="http://unspecified.wordpress.com/2012/02/12/how-do-you-escape-a-complete-uri/" rel="noreferrer">blog post</a> which answers this question in a lot of detail.</p> <p>You should <strong>never</strong> use <code>encodeURI</code> to construct a URI programmatically, for the reasons you say -- you should always use <code>encodeURIComponent</code> on the individual components, and then compose them into a complete URI.</p> <p>Where <code>encodeURI</code> is <em>almost</em> useful is in "cleaning" a URI, in accordance with <a href="http://en.wikipedia.org/wiki/Robustness_principle" rel="noreferrer">Postel's Law</a> ("Be liberal in what you accept, and conservative in what you send.") If someone gives you a complete URI, it may contain illegal characters, such as spaces, certain ASCII characters (such as double-quotes) and Unicode characters. <code>encodeURI</code> can be used to convert those illegal characters into legal percent-escaped sequences, without encoding delimiters. Similarly, <code>decodeURI</code> can be used to "pretty-print" a URI, showing percent-escaped sequences as technically-illegal bare characters.</p> <p>For example, the URL:</p> <pre><code>http://example.com/admin/login?name=Helen Ødegård&amp;gender=f </code></pre> <p>is illegal, but it is still completely unambiguous. <code>encodeURI</code> converts it into the valid URI:</p> <pre><code>http://example.com/admin/login?name=Helen%20%C3%98deg%C3%A5rd&amp;gender=f </code></pre> <p>An example of an application that might want to do this sort of "URI cleaning" is a web browser. When you type a URL into the address bar, it should attempt to convert any illegal characters into percent-escapes, rather than just having an error. Software that processes URIs (e.g., an HTML scraper that wants to get all the URLs in hyperlinks on a page) may also want to apply this kind of cleaning in case any of the URLs are technically illegal.</p> <p>Unfortunately, <code>encodeURI</code> has a critical flaw, which is that it escapes '%' characters, making it completely useless for URI cleaning (it will double-escape any URI that already had percent-escapes). I have therefore borrowed <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURI" rel="noreferrer">Mozilla's fixedEncodeURI</a> function and improved it so that it correctly cleans URIs:</p> <pre><code>function fixedEncodeURI(str) { return encodeURI(str).replace(/%25/g, '%').replace(/%5B/g, '[').replace(/%5D/g, ']'); } </code></pre> <p>So you should always use <code>encodeURIComponent</code> to construct URIs internally. You should only never use <code>encodeURI</code>, but you can use my <code>fixedEncodeURI</code> to attempt to "clean up" URIs that have been supplied from an external source (usually as part of a user interface).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload