通过url传递对象,需要将对象转换为JSON字符串
1.html内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>点击跳转到第二页</title>
</head>
<body>
点击跳转到第二页
<script>
var obj = {
"name":"syy",
"age":24
};
//通过url传递(需要将对象转换为JSON字符串)
document.onclick = ()=>{
window.open("./2.html?tag="+JSON.stringify(obj))
}
</script>
</body>
</html>
2.html内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>第二页</title>
</head>
<body>
第二页
<script>
let query = window.location.href.split('?')[1]
console.log(query);//tag={%22name%22:%22%E7%89%B5One%22,%22age%22:24}
query = decodeURI(query); //转码
console.log(query); //转码后的参数: tag={"name":"syy","age":24}
var transferJson = query.split('=')[1]
//如有特殊符号被替换成编码,可用正则替换,transferJson.replace(/%3A/g,':')
console.log(transferJson) //打印即可得到json字符串,需要对象JSON.parse()即可
</script>
</body>
</html>