编码
escape主要针对字符串编码,encodeURI主要针对url编码,escape可以解析unicode字符串,如下:
var str = '\u6211\u662f\u5c0f\u660e'
function decode_unicode(str){
return unescape(str.replace(/\u/g, "%u"))
}
decode_unicode(str)
encodeURI
encodeURIComponent和encodeURI类似,只是支持的字符更加全面,建议使用encodeURIComponent
解码
unescape
decodeURI
decodeURIComponent
转base64
btoa utf-8转换为base64编码
function utf8_to_base64(str){
return window.btoa(unescape(encodeURIComponent(str)))
}
atob base64转换为utf-8
function base64_to_utf8(str){
return decodeURIComponent(escape(window.atob(str)))
}