11 html javascript
这是login.html的代码:
<ul>
<li class="introduction">
<a href="introduction.html">
<span class="icon"></span>
Introduction
</a>
</li>
<li class="login active">
<a href="#">
<span class="login"></span>
Login
</a>
</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
外部链接将首先使用我想要保留的一些查询参数进入当前页面(登录).如果用户点击第一个链接introduction,则会加载introduction.html.但是,前一页(登录)的所有查询参数都将丢失.
无论如何我可以在保持查询参数的同时加载此页面吗?使用HTML或Javascript.
非常感谢.
Den*_*ret 18
您感兴趣的URL部分称为搜索.
您可以使用相同的参数链接到另一个页面
<a onclick="window.location='someotherpage'+window.location.search;">Login</a>
Run Code Online (Sandbox Code Playgroud)
通过标签上的rel=""属性<a>(使用 jQuery)自动将您当前的参数附加到您认为值得的任何链接:
<a href="introduction.html" rel="keep-params">Link</a>
Run Code Online (Sandbox Code Playgroud)
// all <a> tags containing a certain rel=""
$("a[rel~='keep-params']").click(function(e) {
e.preventDefault();
var params = window.location.search,
dest = $(this).attr('href') + params;
// in my experience, a short timeout has helped overcome browser bugs
window.setTimeout(function() {
window.location.href = dest;
}, 100);
});
Run Code Online (Sandbox Code Playgroud)
修改所有a标签客户端以包含查询参数
[...document.querySelectorAll('a')].forEach(e=>{
//add window url params to to the href's params
const url = new URL(e.href)
for (let [k,v] of new URLSearchParams(window.location.search).entries()){
url.searchParams.set(k,v)
}
e.href = url.toString();
})
Run Code Online (Sandbox Code Playgroud)
注意:如果您在此脚本运行后以编程方式添加新a标签,它将不会更新它,因此它不适用于 SPA。