url get 传参数
解析utf8字元(中文)
发生
Uncaught DOMException: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.
没想到经过网址get传参数
编码后的"+",这个字元会因为searchParams.get(),而导致被替换成" "(空白字元)
// 使用utf-8字符集进行base64编码function utoa(Object) { btoa(unescape(encodeURIComponent(JSON.stringify(Object))))}// https://example.com?data=eyJ0aXRsZSI6IuaWsOWMl+eSsOeLgOe3muW9ouixoeW7o+WRiiJ9// 使用utf-8字符集解析base64字符串 function atou() { githubURL = new URL(window.location); JSON.parse(decodeURIComponent(escape(atob( githubURL .searchParams .get('data') .replace(/-| /g, "+") .replace(/_/g, "/") .replace(/%/g, '%25') ))));}
参考来源:
https://matthung0807.blogspot.com/2020/03/javascript-base64-encode-decode.htmlhttp://levy.work/2017-03-24-black-magic-js-atob-with-utf8/https://www.jianshu.com/p/82afa633033e