我几乎没有可以采用这种格式的字符串:
const s1 = "https://test1.com/testPage";
const s2 = "https://test2.com/testPage?specificParam=1"
const s3 = "https://test3.com/someOtherPage?specificParam=2&restParam=3";
因此,当我在函数中读取它时,我必须检查字符串是否包含查询参数,如果是,那么我必须使用它
window.btoa()
对所有查询参数进行编码。如果没有直接重定向。有人可以协助如何做到这一点吗?
function test(path){
const href = window.location.origin + path;
if(href.includes('?')){
// split all the query params and then attach `btoa` to it and then openthe url
with encoded params
}
window.open(href, '_blank', 'no-referer');
}
您可以使用
URL
构造函数。
const url = new URL("https://test3.com/someOtherPage?specificParam=2&restParam=3");
if (url.search) {
// use url.search or url.searchParams
// base64 encode each query param value
url.search = new URLSearchParams(url.search.slice(1).split('&').map(p => {
const [k, v] = p.split('=');
return [k, btoa(v)];
}));
}