javascript的3种编码函数

第一步:

 

javascript中有3个可以对字符串进行编码的函数:

 

1.escape()函数:该方法不会对ASCII字母和数字进行编码,也不会对下面这些ASCII标点符号进行编码: - _ . ! ~ * ' ( ) .其他所有的字符都会被转义序列替换.

 

2.encodeURI()函数:该方法不会对ASCII字母和数字进行编码,也不会对下面这些ASCII标点符号进行编码: - _ . ! ~ * ' ( ) .该方法的目的是对URI进行完整编码,因此对以下在URI中有特殊意义的ASCII标点符号,encodeURI是不会转义的: ; / ? : @ & = + $ , #

 

3.encodeURIComponent()函数:该方法不会对ASCII字母和数字进行编码,也不会对下面这些ASCII标点符号进行编码: - _ . ! ~ * ' ( ) .其他字符,比如; / ? : @ & = + $ , #都是由一个或多个十六进制的转义序列替换的.

 

第二步:

 

1.上面的是网上的摘抄,具体我们看例子,有图有真相,请以例子为准:

<body>
<script type="text/javascript">
document.write("<h3 style='color:red'>http://www.theseach.com</h3>");
document.write("<h3 style='color:red'>http://www.theseach.com.cn/Xiu Xiu/</h3>");
document.write("<h3 style='color:red'>-_.!~*'()</h3>");
document.write("<h3 style='color:red'>;/?:@&=+$,#</h3>");
document.write("<h3>1.escape()函数用法:</h3>");
document.write(escape("http://www.theseach.com") + "<br />");
document.write(escape("http://www.theseach.com.cn/Xiu Xiu/") + "<br />");
document.write(escape("-_.!~*'()") + "<br />");
document.write(escape(";/?:@&=+$,#") + "<br />");
document.write("<br />");
document.write("<h3>2.encodeURI()函数用法:</h3>");
document.write(encodeURI("http://www.theseach.com")+ "<br />");
document.write(encodeURI("http://www.theseach.com.cn/Xiu Xiu/") + "<br />");
document.write(encodeURI("-_.!~*'()") + "<br />");
document.write(encodeURI(";/?:@&=+$,#") + "<br />");
document.write("<br />");
document.write("<h3>3.encodeURIComponent()函数用法:</h3>");
document.write(encodeURIComponent("http://www.theseach.com") + "<br />");
document.write(encodeURIComponent("http://www.theseach.com.cn/Xiu Xiu/") + "<br />");
document.write(encodeURIComponent("-_.!~*'()") + "<br />");
document.write(encodeURIComponent(";/?:@&=+$,#") + "<br />");
</script>
</body>

 

2.结果如下图所示:

 

参考资料:

1.http://comeonbabye.iteye.com/blog/1193498

2.http://bbs.itheima.com/thread-18278-1-1.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值