mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-08 23:14:06 +08:00
147 lines
8.2 KiB
HTML
147 lines
8.2 KiB
HTML
<article id="wikiArticle">
|
||
<div></div>
|
||
<p><code><strong>encodeURI()</strong></code> 函数通过将特定字符的每个实例替换为一个、两个、三或四转义序列来对统一资源标识符 (URI) 进行编码 (该字符的 UTF-8 编码仅为四转义序列)由两个 "代理" 字符组成)。</p>
|
||
<h2 id="语法">语法</h2>
|
||
<pre><code class="language-javascript"><code>encodeURI(<em>URI</em>)</code></code></pre>
|
||
<h3 id="参数">参数</h3>
|
||
<dl>
|
||
<dt><code>URI</code></dt>
|
||
<dd>一个完整的URI.</dd>
|
||
<dt>
|
||
<h3 id="返回值">返回值</h3>
|
||
<p> 一个新字符串, 表示提供的字符串编码为统一资源标识符 (URI)。</p>
|
||
</dt>
|
||
</dl>
|
||
<h2 id="描述">描述</h2>
|
||
<p>假定一个URI是完整的URI,那么无需对那些保留的并且在URI中有特殊意思的字符进行编码。</p>
|
||
<pre><code>http://username:password@www.example.com:80/path/to/file.php?foo=316&bar=this+has+spaces#anchor</code></code></pre>
|
||
<p><code>encodeURI</code> 会替换所有的字符,但不包括以下字符,即使它们具有适当的UTF-8转义序列:</p>
|
||
<table class="standard-table">
|
||
<tbody>
|
||
<tr>
|
||
<td class="header">类型</td>
|
||
<td class="header">包含</td>
|
||
</tr>
|
||
<tr>
|
||
<td>保留字符</td>
|
||
<td><code>;</code> <code>,</code> <code>/</code> <code>?</code> <code>:</code> <code>@</code> <code>&</code> <code>=</code> <code>+</code> <code>$</code></td>
|
||
</tr>
|
||
<tr>
|
||
<td>非转义的字符</td>
|
||
<td>字母 数字 <code>-</code> <code>_</code> <code>.</code> <code>!</code> <code>~</code> <code>*</code> <code>'</code> <code>(</code> <code>)</code></td>
|
||
</tr>
|
||
<tr>
|
||
<td>数字符号</td>
|
||
<td><code>#</code></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<p>请注意,<code>encodeURI</code> 自身<em>无法</em>产生能适用于HTTP GET 或 POST 请求的URI,例如对于 XMLHTTPRequests, 因为 "&", "+", 和 "=" 不会被编码,然而在 GET 和 POST 请求中它们是特殊字符。然而<a href="Reference/Global_Objects/encodeURIComponent" title="encodeURIComponent()是对统一资源标识符(URI)的组成部分进行编码的方法。它使用一到四个转义序列来表示字符串中的每个字符的UTF-8编码(只有由两个Unicode代理区字符组成的字符才用四个转义字符编码)。"><code>encodeURIComponent</code></a>这个方法会对这些字符编码。</p>
|
||
<p>另外,如果试图编码一个非高-低位完整的代理字符,将会抛出一个 <a href="Reference/Global_Objects/URIError" title="URIError 对象用来表示以一种错误的方式使用全局URI处理函数而产生的错误。"><code>URIError</code></a> 错误,例如:</p>
|
||
<pre><code class="language-javascript">// 编码高-低位完整字符 ok
|
||
console.log(encodeURI('\uD800\uDFFF'));
|
||
|
||
// 编码单独的高位字符抛出 "Uncaught URIError: URI malformed"
|
||
console.log(encodeURI('\uD800'));
|
||
|
||
// 编码单独的低位字符抛出 "Uncaught URIError: URI malformed"
|
||
console.log(encodeURI('\uDFFF'));</code></pre>
|
||
<p>并且需要注意,如果URL需要遵循较新的<a class="external" href="http://tools.ietf.org/html/rfc3986" rel="noopener">RFC3986</a>标准,那么方括号是被保留的(给IPv6),因此对于那些没有被编码的URL部分(例如主机),可以使用下面的代码:</p>
|
||
<pre><code class="language-javascript">function fixedEncodeURI (str) {
|
||
return encodeURI(str).replace(/%5B/g, '[').replace(/%5D/g, ']');
|
||
}</code></pre>
|
||
<h2 id="规范">规范</h2>
|
||
<table class="standard-table">
|
||
<tbody>
|
||
<tr>
|
||
<th scope="col">规范</th>
|
||
<th scope="col">状态</th>
|
||
<th scope="col">备注</th>
|
||
</tr>
|
||
<tr>
|
||
<td><a class="external" href="https://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%201999.pdf" hreflang="en" lang="en" rel="noopener" title="ECMAScript 3rd Edition (ECMA-262)">ECMAScript 3rd Edition (ECMA-262)</a></td>
|
||
<td><span class="spec-Standard">Standard</span></td>
|
||
<td>初始定义</td>
|
||
</tr>
|
||
<tr>
|
||
<td><a class="external" href="https://www.ecma-international.org/ecma-262/5.1/#sec-15.1.3.3" hreflang="en" lang="en" rel="noopener">ECMAScript 5.1 (ECMA-262)<br/><small lang="zh-CN">encodeURI</small></a></td>
|
||
<td><span class="spec-Standard">Standard</span></td>
|
||
<td> </td>
|
||
</tr>
|
||
<tr>
|
||
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-encodeuri-uri" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">encodeURI</small></a></td>
|
||
<td><span class="spec-Standard">Standard</span></td>
|
||
<td> </td>
|
||
</tr>
|
||
<tr>
|
||
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-encodeuri-uri" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">encodeURI</small></a></td>
|
||
<td><span class="spec-Draft">Draft</span></td>
|
||
<td> </td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<h2 id="浏览器兼容性">浏览器兼容性</h2>
|
||
<p></p><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
|
||
This compatibility table still uses the old format,
|
||
because we haven't yet converted the data it contains.
|
||
<strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
|
||
<div class="htab">
|
||
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
|
||
<ul>
|
||
<li class="selected"><a>Desktop</a></li>
|
||
<li><a>Mobile</a></li>
|
||
</ul>
|
||
</div><p></p>
|
||
<div id="compat-desktop">
|
||
<table class="compat-table">
|
||
<tbody>
|
||
<tr>
|
||
<th>特性</th>
|
||
<th>Chrome</th>
|
||
<th>Firefox (Gecko)</th>
|
||
<th>Internet Explorer</th>
|
||
<th>Opera</th>
|
||
<th>Safari</th>
|
||
</tr>
|
||
<tr>
|
||
<td>基础功能</td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<div id="compat-mobile">
|
||
<table class="compat-table">
|
||
<tbody>
|
||
<tr>
|
||
<th>特性</th>
|
||
<th>Android</th>
|
||
<th>Chrome for Android</th>
|
||
<th>Firefox Mobile (Gecko)</th>
|
||
<th>IE Mobile</th>
|
||
<th>Opera Mobile</th>
|
||
<th>Safari Mobile</th>
|
||
</tr>
|
||
<tr>
|
||
<td>基础功能</td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<h2 id="相关链接">相关链接</h2>
|
||
<ul>
|
||
<li><a href="Reference/Global_Objects/decodeURI" title="decodeURI() 函数解码一个由encodeURI 先前创建的统一资源标识符(URI)或类似的例程。"><code>decodeURI</code></a></li>
|
||
<li><a href="Reference/Global_Objects/encodeURIComponent" title="encodeURIComponent()是对统一资源标识符(URI)的组成部分进行编码的方法。它使用一到四个转义序列来表示字符串中的每个字符的UTF-8编码(只有由两个Unicode代理区字符组成的字符才用四个转义字符编码)。"><code>encodeURIComponent</code></a></li>
|
||
<li><a href="Reference/Global_Objects/decodeURIComponent" title="decodeURIComponent() 方法用于解码由 encodeURIComponent 方法或者其它类似方法编码的部分统一资源标识符(URI)。"><code>decodeURIComponent</code></a></li>
|
||
</ul>
|
||
</article> |