使用单击函数javascript将参数传递到另一个html页面



我有一个page1.html页面,并使用重定向函数将其重定向到另一个page2.html页面

第1页

...
<a id="link1" href="page2.html" hidden></a>
<input type="button" name="load1" id="load1" onclick="redirect1(2020);" value="page2"/>
...
<script>
function redirect1(argument1){
$('#link1')[0].click(function(){
}); 
}
</script>
...

第2页

...
<input name="test2" id="test2" value="0"/>
...

我想做的是将一个argument1传递给page2,并将其指定为输入test2值。我找不到这两个页面之间的连接,因为重定向页面时链接断开了。知道吗?

有多种方法可以做到这一点。

  1. 使用查询参数
  2. 使用数据(localStorage、sessionStoreage(

//使用本地存储

// Page 1
$('#load1').click(function(){
localStorage.setItem("username", "some name")
});
// page 2
$(function() {
// get data
const username  = localStorage.getItem("username")
})

使用查询参数

// If data is small, can use query params
// Page 1
$('#load1').click(function(){
// redirect here with query param
window.location = "/page2.html" + "?usename=" + "some name"
});

// Page 2
$(function() {
// get data
const username  = localStorage.getItem("username")
// simply get query param, can write logic and see other code to get param
const username = window.location.split("?usename=")[1] 
})

最新更新