escape,encodeURI和encodeRUIComponent的区别

来源:互联网 发布:centos freebsd 服务器 编辑:程序博客网 时间:2024/05/29 19:49

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

区别在于:

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

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

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

例如:

[javascript] view plaincopy
  1. var url = 'http://www.oschina.net/project/search?q=tomcat';   
  2. var results = ['URI: ' + url];   
  3. // escape  
  4. results.push('escape: ' + escape(url));  
  5. // encodeURI  
  6. results.push('encodeURI: ' + encodeURI(url));  
  7. // encodeURIComponent  
  8. results.push('encodeURIComponent: ' + encodeURIComponent(url));   
  9. document.write(results.join(''));  
  10.    
  11. /* 
  12. URI: http://labs.phpz.org/jstest/null.html?a=TEST1&b=hello world# 
  13. escape: http%3A//labs.phpz.org/jstest/null.html%3Fa%3DTEST1%26b%3Dhello%20world%23 
  14. encodeURI: http://labs.phpz.org/jstest/null.html?a=TEST1&b=hello%20world# 
  15. encodeURIComponent: http%3A%2F%2Flabs.phpz.org%2Fjstest%2Fnull.html%3Fa%3DTEST1%26b%3Dhello%20world%23 
  16. */ 
0 0
原创粉丝点击