PHP前端开发

js如何带参数跳转页面跳转

百变鹏仔 3天前 #JavaScript
文章标签 跳转
javascript 中带参数跳转页面的方法有三种:window.location.href、window.location.replace 和 window.open,分别用于创建新历史记录、替换当前历史记录和在新窗口或标签页中打开页面。目标页面可通过 window.location.search 获取传递的参数值。

JavaScript 带参数跳转页面

在 JavaScript 中,可以通过以下方式带参数跳转页面:

方法:

1. window.location.href

window.location.href = "newpage.html?param1=value1&param2=value2";

2. window.location.replace

window.location.replace("newpage.html?param1=value1&param2=value2");

区别:

3. window.open

window.open("newpage.html?param1=value1&param2=value2");

区别:

获取参数值:

在目标页面中,可以使用 window.location.search 获取传递的参数值:

const params = new URLSearchParams(window.location.search);const param1 = params.get("param1");const param2 = params.get("param2");