mozilla

Revision 319783 of encodeURI()

  • Revision slug: JavaScript/Reference/Global_Objects/encodeURI
  • Revision title: encodeURI
  • Revision id: 319783
  • Created:
  • Creator: ethertank
  • Is current revision? No
  • Comment CleanUp only.
Tags: 

Revision Content

Summary

Encodes a Uniform Resource Identifier (URI) by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two "surrogate" characters).

Syntax

encodeURI(URI)

Parameters

URI
A complete Uniform Resource Identifier.

Description

Assumes that the URI is a complete URI, so does not encode reserved characters that have special meaning in the URI.

encodeURI replaces all characters except the following with the appropriate UTF-8 escape sequences:

Type Includes
Reserved characters ; , / ? : @ & = + $
Unescaped characters alphabetic, decimal digits, - _ . ! ~ * ' ( )
Score #

Note that encodeURI by itself cannot form proper HTTP GET and POST requests, such as for XMLHTTPRequests, because "&", "+", and "=" are not encoded, which are treated as special characters in GET and POST requests. encodeURIComponent, however, does encode these characters. These behaviors are most likely not consistent across browsers.

Also note that if one wishes to follow the more recent RFC3986 for URL's, making square brackets reserved (for IPv6) and thus not encoded when forming something which could be part of a URL (such as a host), the following may help. See http://labs.apache.org/webarch/uri/rfc/rfc3986.html

function fixedEncodeURI (str) {
    return encodeURI(str).replace(/%5B/g, '[').replace(/%5D/g, ']');
}

See also

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>

<p>Encodes a Uniform Resource Identifier (URI) by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two "surrogate" characters).</p>



<h2 id="Syntax" name="Syntax">Syntax</h2>

<pre class="syntaxbox"><code>encodeURI(<em>URI</em>)</code></pre>


<h3 id="Parameters" name="Parameters">Parameters</h3>
<dl>
  <dt><code>URI</code></dt>
  <dd>A complete Uniform Resource Identifier.</dd>
</dl>



<h2 id="Description" name="Description">Description</h2>
<p>Assumes that the URI is a complete URI, so does not encode reserved characters that have special meaning in the URI.</p>
<p><code>encodeURI</code> replaces all characters except the following with the appropriate UTF-8 escape sequences:</p>
<table class="fullwidth-table">
  <tbody>
    <tr>
      <td class="header">Type</td>
      <td class="header">Includes</td>
    </tr>
    <tr>
      <td>Reserved characters</td>
      <td><code>;</code> <code>,</code> <code>/</code> <code>?</code> <code>:</code> <code>@</code> <code>&amp;</code> <code>=</code> <code>+</code> <code>$</code></td>
    </tr>
    <tr>
      <td>Unescaped characters</td>
      <td>alphabetic, decimal digits, <code>-</code> <code>_</code> <code>.</code> <code>!</code> <code>~</code> <code>*</code> <code>'</code> <code>(</code> <code>)</code></td>
    </tr>
    <tr>
      <td>Score</td>
      <td><code>#</code></td>
    </tr>
  </tbody>
</table>

<p>Note that <code>encodeURI</code> by itself <em>cannot</em> form proper HTTP GET and POST requests, such as for XMLHTTPRequests, because "&amp;", "+", and "=" are not encoded, which are treated as special characters in GET and POST requests. <code><a href="/en-US/docs/JavaScript/Reference/Global_Objects/encodeURIComponent" title="JavaScript/Reference/Global_Objects/encodeURIComponent">encodeURIComponent</a></code>, however, does encode these characters. These behaviors are most likely not consistent across browsers.</p>

<p>Also note that if one wishes to follow the more recent RFC3986 for URL's, making square brackets reserved (for IPv6) and thus not encoded when forming something which could be part of a URL (such as a host), the following may help. See <a class="external" href="http://labs.apache.org/webarch/uri/rfc/rfc3986.html" rel="freelink">http://labs.apache.org/webarch/uri/rfc/rfc3986.html</a></p>

<pre class="brush: js">
function fixedEncodeURI (str) {
    return encodeURI(str).replace(/%5B/g, '[').replace(/%5D/g, ']');
}
</pre>


<h2 id="See_also" name="See_also">See also</h2>
<ul>
<li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/decodeURI" title="JavaScript/Reference/Global_Functions/decodeURI">decodeURI</a></li>
<li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/decodeURIComponent" title="JavaScript/Reference/Global_Objects/decodeURIComponent">decodeURIComponent</a></li>
<li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/encodeURIComponent" title="JavaScript/Reference/Global_Objects/encodeURIComponent">encodeURIComponent</a></li>
</ul>

<!--languages({
	"fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Fonctions_globales/encodeURI",
} ) -->
Revert to this revision