escape,encodeURI和encodeRUIComponent的區別

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. */ 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章