window.location.href跳转页面如何带参?一分钟教会你

前端跳转到另一个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'));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值