var encodedData = btoa('Hello, world'); // encode a string
var decodedData = atob(encodedData); // decode the string
function utoa(str) {
return window.btoa(unescape(encodeURIComponent(str)));
}
// base64 encoded ascii to ucs-2 string
function atou(str) {
return decodeURIComponent(escape(window.atob(str)));
}
// 使用:
utoa('✓ à la mode'); // 4pyTIMOgIGxhIG1vZGU=
atou('4pyTIMOgIGxhIG1vZGU='); // "✓ à la mode"
utoa('I \u2661 Unicode!'); // SSDimaEgVW5pY29kZSE=
atou('SSDimaEgVW5pY29kZSE='); // "I ♡ Unicode!"
總結一下
btoa 將普通字符串轉化為base64編碼
atou base64編碼轉化為普通字符串
escape 可對字符串進行編碼
unescape 可對字符串進行解碼
encodeURIComponent(URIstring) 把字符串作為URI組件進行編碼。
URIstring 必需。一個字符串,含有URI組件或其他要編碼的文本。
decodeURIComponent 把字符串作為URI組件進行解碼。
URIstring 必需。一個字符串,含有URI組件或其他要編碼的文本。
注:關於URL和URI的區別可以看看這個。
來源:https://blog.csdn.net/momDIY/article/details/78329718