当前位置:编程学习 > JS >>

完美的js URLEncode函数

当需要通过查询字符串传值给服务器时需要对get参数进行encode。
放置中文和特殊符号乱码的代码
escape()函数,不会encode @*/+ (不推荐使用)
encodeURI()函数,不会encode ~!@#$&*()=:/,;?+' (不推荐使用)
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, '+');
}
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,