Javascript在页面更改时保留URL参数

Javascript在页面更改时保留URL参数,javascript,query-string,window.location,Javascript,Query String,Window.location,我使用的是香草Javascript 我有两个页面,page01.html和page02.html。这些页面共享相同的导航系统 我想在page01的url中加载一个参数,在导航中单击page02后,page02将在其url中加载该参数 例如: 我在page01.html上 我将一个参数加载到URL中,因此现在有了page01.html/?param=X 我单击菜单项page02 我想加载page02.html/?param=X 有没有不使用localStorage和sessionStorage的方

我使用的是香草Javascript

我有两个页面,page01.html和page02.html。这些页面共享相同的导航系统

我想在page01的url中加载一个参数,在导航中单击page02后,page02将在其url中加载该参数

例如:

我在page01.html上 我将一个参数加载到URL中,因此现在有了page01.html/?param=X 我单击菜单项page02 我想加载page02.html/?param=X
有没有不使用localStorage和sessionStorage的方法可以做到这一点?

这里有一个使用vanilla JavaScript的简单解决方案:

Page01.html

Page02.html


我使用了贝基的评论和亨利的回答来创建我自己的版本

基本上我只是创建了一个onclick=menuClick;标签上的id=menuLink。 然后在Javascript上,我刚刚将location.search添加到href:

menuClick = function() {
    document.getElementById('menuLink').href += location.search
  }
只需在菜单链接el上el.href+=location.search即可
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>

<div>Page 2</div>

<a href="page01.html">go to page 1</a>

<script>
    var linkToPage1 = document.querySelector("a[href='page01.html']");
    linkToPage1.addEventListener("click", function(e){
     e.preventDefault();
        if(location.search){ 
             window.location.href = "page01.html" + location.search;
         }
     });
 </script>
 </body>
 </html>
menuClick = function() {
    document.getElementById('menuLink').href += location.search
  }