js实现页面带参跳转
使用URLSearchParams添加参数
通过URLSearchParams对象构建查询字符串,动态拼接参数到目标URL。这种方法适合现代浏览器,支持自动编码特殊字符:
const params = new URLSearchParams();
params.append('key1', 'value1');
params.append('key2', 'value2');
window.location.href = `target.html?${params.toString()}`;
直接拼接URL字符串
手动拼接URL和参数,适用于简单场景。需注意用encodeURIComponent对参数值编码以避免语法错误:

const key1 = encodeURIComponent('value1');
const key2 = encodeURIComponent('value2');
window.location.href = `target.html?key1=${key1}&key2=${key2}`;
通过表单提交跳转
使用隐藏表单的GET方法提交参数,适合需要模拟表单跳转的场景:

const form = document.createElement('form');
form.method = 'GET';
form.action = 'target.html';
const input1 = document.createElement('input');
input1.type = 'hidden';
input1.name = 'key1';
input1.value = 'value1';
form.appendChild(input1);
document.body.appendChild(form);
form.submit();
使用history.pushState(无刷新跳转)
通过History API修改当前URL并添加参数,不触发页面刷新,适合单页应用(SPA):
const params = new URLSearchParams();
params.set('key1', 'value1');
history.pushState({}, '', `${window.location.pathname}?${params}`);
通过sessionStorage或localStorage传递参数
非URL参数传递方式,适合复杂数据或隐私敏感场景:
sessionStorage.setItem('tempKey1', 'value1');
window.location.href = 'target.html';
// 在target.html中通过sessionStorage.getItem('tempKey1')获取
注意事项
- 参数值需用
encodeURIComponent处理特殊字符(如&,=)。 - URL长度限制因浏览器而异,通常不超过2000字符。
history.pushState需配合popstate事件监听处理回退操作。






