完美的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, '+'); }
