<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[沧海一粟]]></title> 
<link>http://www.dzhope.com/index.php</link> 
<description><![CDATA[Web系统架构与服务器运维,php开发]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[沧海一粟]]></copyright>
<item>
<link>http://www.dzhope.com/post//</link>
<title><![CDATA[escape,encodeURI,encodeURIComponent函数比较]]></title> 
<author>jed &lt;jed521@163.com&gt;</author>
<category><![CDATA[代码编程]]></category>
<pubDate>Fri, 26 Sep 2008 02:45:19 +0000</pubDate> 
<guid>http://www.dzhope.com/post//</guid> 
<description>
<![CDATA[ 
	js对文字进行编码涉及3个函数：escape,encodeURI,encodeURIComponent，相应3个解码函数：unescape,decodeURI,decodeURIComponent<br/><br/>1、&nbsp;&nbsp;传递参数时需要使用encodeURIComponent，这样组合的url才不会被#等特殊字符截断。&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br/><br/>例如：<br/><br/><div class="code"><br/><br/>&lt;script language=&quot;javascript&quot;&gt;document.write(&#039;&lt;a href=&quot;http://passport.baidu.com/?logout&amp;aid=7&amp;u=&#039;+encodeURIComponent(&quot;http://cang.baidu.com/bruce42&quot;)+&#039;&quot;&gt;退出&lt;/a&gt;&#039;);&lt;/script&gt;<br/><br/></div><br/><br/>2、&nbsp;&nbsp;进行url跳转时可以整体使用encodeURI<br/><br/>例如： Location.href=encodeURI(<a href="http://cang.baidu.com/do/s?word=" target="_blank">http://cang.baidu.com/do/s?word=</a>中国asp之家&ct=21);<br/>3、&nbsp;&nbsp;js使用数据时可以使用escape<br/><br/>例如：搜藏中history纪录。<br/><br/>4、&nbsp;&nbsp;escape对0-255以外的unicode值进行编码时输出%u****格式，其它情况下escape，encodeURI，encodeURIComponent编码结果相同。<br/><br/>最多使用的应为encodeURIComponent，它是将中文、韩文等特殊字符转换成utf-8格式的url编码，所以如果给后台传递参数需要使用encodeURIComponent时需要后台解码对utf-8支持（form中的编码方式和当前页面编码方式相同）<br/><br/>（中国asp之家注：就是说如果你的页面编码是gb2312的话，服务器端接收的将是乱码）<br/><br/>escape不编码字符有69个：*，+，-，.，/，@，_，0-9，a-z，A-Z<br/><br/>encodeURI不编码字符有82个：!，#，$，&，'，(，)，*，+，,，-，.，/，:，;，=，?，@，_，~，0-9，a-z，A-Z<br/><br/>encodeURIComponent不编码字符有71个：!， '，(，)，*，-，.，_，~，0-9，a-z，A-Z<br/><br/>附上这个三个函数的介绍:<br/><br/>escape 方法<br/><br/>对 String 对象编码以便它们能在所有计算机上可读， <br/><br/>escape(charString)<br/>必选项 charstring 参数是要编码的任意 String 对象或文字。 <br/><br/>说明<br/>escape 方法返回一个包含了 charstring 内容的字符串值（ Unicode 格式）。所有空格、标点、重音符号以及其他非 ASCII 字符都用 %xx 编码代替，其中 xx 等于表示该字符的十六进制数。例如，空格返回的是 "%20" 。<br/><br/>字符值大于 255 的以 %uxxxx 格式存储。 <br/><br/>注意&nbsp;&nbsp; escape 方法不能够用来对统一资源标示码 (URI) 进行编码。对其编码应使用 encodeURI 和encodeURIComponent 方法。<br/><br/>encodeURI 方法<br/><br/>将文本字符串编码为一个有效的统一资源标识符 (URI)。<br/><br/>encodeURI(URIString)<br/>必选的 URIString 参数代表一个已编码的 URI。<br/><br/>说明<br/>encodeURI 方法返回一个编码的 URI。如果您将编码结果传递给 decodeURI，那么将返回初始的字符串。encodeURI 方法不会对下列字符进行编码：":"、"/"、";" 和 "?"。请使用 encodeURIComponent 方法对这些字符进行编码。<br/><br/>encodeURIComponent 方法<br/><br/>将文本字符串编码为一个统一资源标识符 (URI) 的一个有效组件。<br/><br/>encodeURIComponent(encodedURIString)<br/>必选的 encodedURIString 参数代表一个已编码的 URI 组件。<br/><br/>说明<br/><br/>encodeURIComponent 方法返回一个已编码的 URI。如果您将编码结果传递给 decodeURIComponent，那么将返回初始的字符串。因为 encodeURIComponent 方法对所有的字符编码，请注意，如果该字符串代表一个路径，例如 /folder1/folder2/default.html，其中的斜杠也将被编码。这样一来，当该编码结果被作为请求发送到 web 服务器时将是无效的。如果字符串中包含不止一个 URI 组件，请使用 encodeURI 方法进行<br/><br/><br/>Tags - <a href="http://www.dzhope.com/tags/js/" rel="tag">js</a> , <a href="http://www.dzhope.com/tags/%25E7%25BC%2596%25E7%25A0%2581/" rel="tag">编码</a> , <a href="http://www.dzhope.com/tags/url%25E5%258A%25A0%25E5%25AF%2586/" rel="tag">url加密</a>
]]>
</description>
</item><item>
<link>http://www.dzhope.com/post//#blogcomment</link>
<title><![CDATA[[评论] escape,encodeURI,encodeURIComponent函数比较]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.dzhope.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>