我想创建一个具有动态标题的网页,该标题根据我在另一个页面中单击的链接而变化。
假设我有一个包含两个链接的页面
<a href="somePage.html">link 1</a>
<a href="somePage.html">link 2</a>
在"somePage.html“中,我有一个标题,我想根据我点击的链接(链接1或链接2)修改它。
<h2>you were directed from [here I should insert link 1 or link 2] </h2>
我只能使用html,css和JS,逻辑必须在前端。
有办法做到这一点吗?
发布于 2020-09-27 21:31:23
也许你可以给每个链接添加查询参数:
<a href="somePage.html?page=link 1">link 1</a>
<a href="somePage.html?page=link 2">link 2</a>
<div id='redirected-from'></div>
<script>
const urlParams = new URLSearchParams(window.location.search);
let text = urlParams.get('page');
// escape &, <, >, " and '
linkText = text.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
document.getElementById("redirected-from").innerHTML = `<h2>You were directed from ${linkText}</h2>`
</script>
https://stackoverflow.com/questions/64088946
复制相似问题