javascript文字编码函数escape,encodeURI,encodeURIComponent使用详解

来源:互联网 发布:淘宝客服聊天记录恢复 编辑:程序博客网 时间:2024/04/28 09:29
javascript文字编码函数escape,encodeURI,encodeURIComponent使用详解
2009年11月15日 星期日 4:00

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

1、传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断。  
                        
例如:
//xmlHttp为xmlHttpRequest对象
var params=''s=''+encodeURIComponent(''测试文字#&等等'')+''&r=''+Math.random();
xmlHttp.open(''post'',target_url,true);
xmlHttp.setrequestheader("content-length",params.length);
xmlHttp.setRequestHeader(''Content-Type'',''application/x-www-form-urlencoded'')
xmlHttp.send(params);

可以在服务器端脚本中接受s参数,输出其值看是否正确.

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

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

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

例如:搜藏中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

escape 方法
对 String 对象编码以便它们能在所有计算机上可读,

escape(charString)

必选项 charstring 参数是要编码的任意 String 对象或文字。

说明
escape 方法返回一个包含了 charstring 内容的字符串值( Unicode 格式)。所有空格、标点、重音符号以及其他非 ASCII 字符都用 %xx 编码代替,其中 xx 等于表示该字符

的十六进制数。例如,空格返回的是 "%20" 。

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

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

encodeURI 方法
将文本字符串编码为一个有效的统一资源标识符 (URI)。

encodeURI(URIString)

必选的 URIString 参数代表一个已编码的 URI。

说明
encodeURI 方法返回一个编码的 URI。如果您将编码结果传递给 decodeURI,那么将返回初始的字符串。encodeURI 方法不会对下列字符进行编码:":"、"/"、";" 和 "?"。请使

用 encodeURIComponent 方法对这些字符进行编码。

encodeURIComponent 方法
将文本字符串编码为一个统一资源标识符 (URI) 的一个有效组件。

encodeURIComponent(encodedURIString)

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

说明
encodeURIComponent 方法返回一个已编码的 URI。如果您将编码结果传递给 decodeURIComponent,那么将返回初始的字符串。因为 encodeURIComponent 方法对所有的字符编码

,请注意,如果该字符串代表一个路径,例如 /folder1/folder2/default.html,其中的斜杠也将被编码。这样一来,当该编码结果被作为请求发送到 web 服务器时将是无效的。

如果字符串中包含不止一个 URI 组件,请使用 encodeURI 方法进行。

关于数组的一个总结:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    
<title>javascript数组学习</title>
    
<scripttype="text/javascript">
        
function debug(o){a=[];for(kin o)a.push(k+":"+o[k]);alert(a.join("\n"))}
        
//javascript数组学习
        function f1(){
             a
=[];//定义一个数组
             a.push("1");//通过push方法向数组中添加数据,追加
             a.push("2");
             a[
2]="001";//通过下标添加
            //alert(a);//默认数组是用","分隔开
            //alert(a.join("\n"));//join的作用是把分隔符替换成"\n"
            //alert(a.join("|"));
            for(kin a){//遍历数组,这里的k会从0到a的长度
                 alert(a[k]+"|"+k);
             }
            
//document.domain="0379zd.com";//只读的
             alert(document.domain);
            
//url编码解码
            var s= encodeURIComponent("http://www.0379zd.com");//编码
             alert(decodeURIComponent(s));//解码
         }
        
function getform(f){
            
if(!f) f=document.forms[0];
            
var s='';
            
for(var i=0;i<f.length;i++){
                
var e=f[i];
                
if(e.id)
                     s
+='&'+e.id+'='+encodeURIComponent(e.value)
             }
            
//return s
             alert(s);
         }
    
</script>
</head>
<body>
<form id="form1" action="" onclick="getform(this);">form</form>
<input type="button" onclick="f1();" value="ok"/>
<input type="button" onclick="debug(this);" value="debug"/>
<div onclick="debug(this);">click</div>
<div>form</div>
</body>
</html>


原创粉丝点击