aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/encodeuri
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/encodeuri')
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/encodeuri/index.html170
1 files changed, 170 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/encodeuri/index.html b/files/zh-cn/web/javascript/reference/global_objects/encodeuri/index.html
new file mode 100644
index 0000000000..8f3db7a445
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/global_objects/encodeuri/index.html
@@ -0,0 +1,170 @@
+---
+title: encodeURI()
+slug: Web/JavaScript/Reference/Global_Objects/encodeURI
+tags:
+ - JavaScript
+ - URI
+ - decodeURI
+ - encodeURI
+ - 统一资源定位符
+translation_of: Web/JavaScript/Reference/Global_Objects/encodeURI
+---
+<div>{{jsSidebar("Objects")}}</div>
+
+<p><code><strong>encodeURI()</strong></code>  函数通过将特定字符的每个实例替换为一个、两个、三或四转义序列来对统一资源标识符 (URI) 进行编码 (该字符的 UTF-8 编码仅为四转义序列)由两个 "代理" 字符组成)。</p>
+
+<h2 id="语法">语法</h2>
+
+<pre class="syntaxbox"><code>encodeURI(<em>URI</em>)</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&amp;bar=this+has+spaces#anchor</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>&amp;</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, 因为 "&amp;", "+", 和 "=" 不会被编码,然而在 GET 和 POST 请求中它们是特殊字符。然而{{jsxref("encodeURIComponent")}}这个方法会对这些字符编码。</p>
+
+<p>另外,如果试图编码一个非高-低位完整的代理字符,将会抛出一个 {{jsxref("URIError")}} 错误,例如:</p>
+
+<pre class="brush: js">// 编码高-低位完整字符 ok
+console.log(encodeURI('\uD800\uDFFF'));
+
+// 编码单独的高位字符抛出 "Uncaught URIError: URI malformed"
+console.log(encodeURI('\uD800'));
+
+// 编码单独的低位字符抛出 "Uncaught URIError: URI malformed"
+console.log(encodeURI('\uDFFF'));</pre>
+
+<p>并且需要注意,如果URL需要遵循较新的<a href="http://tools.ietf.org/html/rfc3986">RFC3986</a>标准,那么方括号是被保留的(给IPv6),因此对于那些没有被编码的URL部分(例如主机),可以使用下面的代码:</p>
+
+<pre class="brush: js">function fixedEncodeURI (str) {
+ return encodeURI(str).replace(/%5B/g, '[').replace(/%5D/g, ']');
+}</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>{{SpecName('ES3')}}</td>
+ <td>{{Spec2('ES3')}}</td>
+ <td>初始定义</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES5.1', '#sec-15.1.3.3', 'encodeURI')}}</td>
+ <td>{{Spec2('ES5.1')}}</td>
+ <td> </td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES6', '#sec-encodeuri-uri', 'encodeURI')}}</td>
+ <td>{{Spec2('ES6')}}</td>
+ <td> </td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-encodeuri-uri', 'encodeURI')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+<p>{{CompatibilityTable}}</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>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</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>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<h2 id="相关链接">相关链接</h2>
+
+<ul>
+ <li>{{jsxref("decodeURI")}}</li>
+ <li>{{jsxref("encodeURIComponent")}}</li>
+ <li>{{jsxref("decodeURIComponent")}}</li>
+</ul>