完美的js URLEncode函数


完美的js URLEncode函数

当需要通过查询字符串传值给服务器时需要对get参数进行encode。

  1. escape()函数,不会encode @*/+ (不推荐使用)
  2. encodeURI()函数,不会encode ~!@#$&*()=:/,;?+' (不推荐使用)
  3. encodeURIComponent()函数,不会encode~!*() 这个函数是最常用的

我们需要对encodeURIComponent函数,最一点修改:

function urlencode (str) { str = (str + '').toString(); return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28'). replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+'); }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM