js中escape,encodeURI,encodeURIComponent三个函数的区别

在WEB(我是在Asp.net环境,相信其它也一样.)开发当中,当你传有中文值的URL(如http://localhost/Test/test.aspx?name=张三),你会发现你通过Request.Questring["Name"]服务器处理时获取的并不是你要的"张三",而是一些奇怪的字符,问题就来了。这时候你可用通过Javascript的window.encodeURIComponent方法转换你的URL,将中文部分转换成encode。贴入javascript实现的方法.

 function qs(url)
 { 
   
if (window.RegExp && window.encodeURIComponent)
    {
      
return encodeURIComponent(url); 
    } 
 } 
 

你可以参照GOOGLE的方法:

function qs(el) 
{
  
if (window.RegExp && window.encodeURIComponent)
  {
     
var ue=el.href;var qe=encodeURIComponent(document.gs.q.value);
     
if(ue.indexOf("q=")!=-1)
    {
      el.href
=ue.replace(new RegExp("q=[^&$]*"),"q="+qe);
     }
     
else
    {
      el.href
=ue+"&q="+qe;
    }
  }
  
return 1;
}

js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent

1、   传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断。                            

例如:<script language="javascript">document.write('<a href="http://passport.baidu.com/?logout&aid=7&u='+encodeURIComponent("http://cang.baidu.com/bruce42")+'">退出</a>');</script>

2、   进行url跳转时可以整体使用encodeURI

例如:Location.href=encodeURI("http://cang.baidu.com/do/s?word=百度&ct=21");

3、   js使用数据时可以使用escape

[Huoho.Com编辑]

例如:搜藏中history纪录。

4、   escape对0-255以外的unicode值进行编码时输出%u****格式,其它情况下escape,encodeURI,encodeURIComponent编码结果相同。


最多使用的应为encodeURIComponent,它是将中文、韩文等特殊字符转换成utf-8格式的url编码,所以如果给后台传递参数需要使用encodeURIComponent时需要后台解码对utf-8支持(form中的编码方式和当前页面编码方式相同)

escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z

encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z

encodeURIComponent不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z

对于ASCII字符,这三个函数的作用都是将字符转换成百分比编码(Percent-encoding ),区别是各自排除编码的字符不同:

escape() will not encode: @*/+

encodeURI() will not encode: ~!@#$&*()=:/,;?+'

encodeURIComponent() will not encode: ~!*()'

此外,escape其实是window对象的方法 ,encodeURIComponent和encodeURI是JS内置函数。

MDC里的资料:window.escapeGlobal_Functions/encodeURIComponent .

对于非ASCII字符,escape和encodeURIComponent差异比较大:

escape('雕') == '%u96D5'

encodeURIComponent('雕') == '%E9%9B%95'

%u96D5是非标准Pecent-encoding, 现在已经没有标准支持。

对于这种反转码最简单的办法,是用unescape:

alert(unescape('%u96D5');

escape 方法

返回一个可在所有计算机上读取的编码 String 对象。

function escape(charString : String) : String

参数

charString

必选。要编码的任何 String 对象或文本。

备注

escape 方法返回一个包含 charstring 内容的字符串值(Unicode 格式)。所有空格、标点、重音符号以及任何其他非 ASCII 字符都用 %xx 编码替换,其中 xx 等于表示该字符的十六进制数。例如,空格返回为“%20”。

字符值大于 255 的字符以 %uxxxx 格式存储。

注意 escape 方法不能用来对“统一资源标识符”(URI) 进行编码。对其编码应使用 encodeURI 和 encodeURIComponent 方法。

要求

版本 1

请参见

encodeURI 方法 | encodeURIComponent 方法 | String 对象 | unescape 方法

适用于:Global 对象

encodeURI 方法

返回编码为有效的统一资源标识符 (URI) 的字符串。

function encodeURI(URIString : String) : String

参数

URIString

必选。表示编码 URI 的字符串。

备注

encodeURI 方法返回一个已编码的 URI。如果将编码结果传递给 decodeURI,则将返回初始的字符串。encodeURI 不对下列字符进行编码:“:”、“/”、“;”和“?”。请使用 encodeURIComponent 对这些字符进行编码。

要求

版本 5.5

请参见

decodeURI 方法 | decodeURIComponent 方法

适用于:Global 对象

encodeURIComponent 方法

返回编码为统一资源标识符 (URI) 的有效组件的字符串。

function encodeURIComponent(encodedURIString : String) : String

参数

encodedURIString

必选。表示编码 URI 组件的字符串。

备注

encodeURIComponent 方法返回一个已编码的 URI。如果将编码结果传递给 decodeURIComponent,则将返回初始的字符串。因为 encodeURIComponent 方法将对所有字符编码,请注意,如果该字符串代表一个路径,例如 /folder1/folder2/default.html,则其中的斜杠也将被编码,这样,当该字符串作为请求发送到 Web 服务器时它将是无效的。如果字符串中包含多个 URI 组件,请使用 encodeURI 方法进行编码。

要求

版本 5.5

请参见

decodeURI 方法 | decodeURIComponent 方法

适用于:Global 对象

unescape 方法

从用 escape 方法编码的 String 对象中返回已解码的字符串。

function unescape(charString : String) : String

参数

charString

必选。要解码的 String 对象或文本。

备注

unescape 方法返回一个包含 charstring 内容的字符串值。所有以 %xx 十六进制形式编码的字符都用 ASCII 字符集当中等效的字符代替。

以 %uxxxx 格式(Unicode 字符)编码的字符用十六进制编码 xxxx 的 Unicode 字符代替。

注意 unescape 方法不应用于解码“统一资源标识符”(URI)。请改用 decodeURI 和 decodeURIComponent 方法。

要求

版本 1

请参见

decodeURI 方法 | decodeURIComponent 方法 | escape 方法 | String 对象

适用于:Global 对象

decodeURI 方法

返回一个已编码的统一资源标识符 (URI) 的非编码形式。

function decodeURI(URIstring : String) : String

参数

URIstring

必选。表示编码 URI 的字符串。

备注

使用 decodeURI 方法代替已经过时的 unescape 方法。

decodeURI 方法返回一个字符串值。

如果 URIString 无效,将发生 URIError。

要求

版本 5.5

请参见

decodeURIComponent 方法 | encodeURI 方法

适用于:Global 对象

decodeURIComponent 方法

返回统一资源标识符 (URI) 的一个已编码组件的非编码形式。

function decodeURIComponent(encodedURIString : String) : String

必选的 encodedURIString 参数是一个表示已编码的 URI 组件的值。

备注

URIComponent 是一个完整的 URI 的一部分。

如果 encodedURIString 无效,则将产生 URIError。

要求

版本 5.5

请参见

decodeURI 方法 | encodeURI 方法

适用于:Global 对象

原文地址:https://www.cnblogs.com/fx2008/p/2295580.html