前端跳转到另一个html页面,相信大家都知道使用window.location.href = 'xxx.html';
但是很多时候我们需要携带一些参数跳转,那该怎么操作呢?
下面让我举几个例子:
// 从当前页面跳转到test.html页面
window.location.href = './test.html'
// 带参数name:"chen"
window.location.href = './test.html?name=chen'
// 带多个参数 用&连接
window.location.href = './test.html?name=chen&age=18'
// 带对象参数obj
let obj = {
name : 'chen',
age : 18
}
window.location.href = './test.html?objdata=' + JSON.stringify(obj);
在跳转后的test.html页面获取到传过来的参数
// 获取 URL 中的查询字符串
let search = window.location.search;
// 将查询字符串解析为 URLSearchParams 对象
let urlParams = new URLSearchParams(search);
let obj = JSON.parse(urlParams.get('objdata'));