页面1
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Page1</title> <script> function jump(){ location.href="./page2.html?id=123&name=page1"; } </script> </head> <body> <button onclick="jump()">jump</button> </body> </html>
页面2
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Page2</title> <script> let map = getParameterMap(); console.log(map.get("id")); console.log(map.get("name")); // 返回参数map function getParameterMap() { let parameters = window.location.search; // 如果没有参数 if (parameters.indexOf("?") == -1) return null; let map = new Map; let strs = parameters.substr(1).split("&"); for (let i = 0; i < strs.length; i++) { let str = strs[i].split("="); map.set(str[0], str[1]); } return map; } </script> </head> <body> </body> </html>
结果