escape,encodeURI和encodeRUIComponent的区别

JavaScript中有三个URL函数编码,他们都是全局的;

区别在于:

encodeURI只将URI中的空格和非AscII字符进行编码,编码后的URI可以正常访问(ajax中文问题可以使用encodeURI对url进行编码)

encodeURIComponent 除了将所有的非ASCII字符编码外,还将一些特殊字符进行编码,如?#:,&等,编码后的URI不可访问

escape 功能和encodeURIComponent功能一样,但是编码后的URI是可以访问的,对使用没有任何影响

例如:

var url = 'http://www.oschina.net/project/search?q=tomcat'; 
var results = ['URI: ' + url]; 
// escape
results.push('escape: ' + escape(url));
// encodeURI
results.push('encodeURI: ' + encodeURI(url));
// encodeURIComponent
results.push('encodeURIComponent: ' + encodeURIComponent(url)); 
document.write(results.join(''));
 
/*
URI: http://labs.phpz.org/jstest/null.html?a=TEST1&b=hello world#
escape: http%3A//labs.phpz.org/jstest/null.html%3Fa%3DTEST1%26b%3Dhello%20world%23
encodeURI: http://labs.phpz.org/jstest/null.html?a=TEST1&b=hello%20world#
encodeURIComponent: http%3A%2F%2Flabs.phpz.org%2Fjstest%2Fnull.html%3Fa%3DTEST1%26b%3Dhello%20world%23
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值