2 回答
TA贡献1895条经验 获得超3个赞
您可以在此处使用本地存储 .i.e: 保存 in 的 id 并使用 和 获取它到其他页面。a hreflocalStoragegetItemsetItem
您将如下所示main.html
<style>
//making button look like a tag
button {
background: none!important;
border: none;
padding: 0!important;
color: #069;
text-decoration: underline;
cursor: pointer;
}
</style>
<button href="https://www.mywebsite/central.htm" id="1" onclick="save(this)">First link will go to the central page first and then go to Destination A</button>
<button href="https://www.mywebsite/central.htm" id="2" onclick="save(this)">Second link will go to the central page first and then go to Destination B</button>
<button href="https://www.mywebsite/central.htm" id="3" onclick="save(this)">Thrid link will go to the central page first and then go to Destination C</button>
<button href="https://www.mywebsite/central.htm" id="4" onclick="save(this)">Fourth link will go to the central page first and then go to Destination D</button>
//^added onclick
<script>
function save(el){
//getting id of href click
var ids=el.getAttribute("id");
console.log(ids);
localStorage.clear();//clear previous data
localStorage.setItem("ids", ids);//add data to storage
var href=el.getAttribute("href");//get href
console.log(href)
window.open(href, '_blank');//open in blank page
}
</script>
然后在你的做如下:central page
function openDestination() {
if (localStorage.getItem("ids") != null) {
//get that value
var ids= localStorage.getItem("ids");
console.log(ids);
}
switch(ids) {
case 1:
window.open("https://www.mywebsite/DestinationA.html");
break;
case 2:
window.open("https://www.mywebsite/DestinationB.html");
break;
case 3:
window.open("https://www.mywebsite/DestinationC.html");
break;
case 4:
window.open("https://www.mywebsite/DestinationD.html");
break;
//if none of those, close the window
default:
window.close();
}
}
TA贡献1836条经验 获得超3个赞
您可以使用网址查询。这意味着您可以在页面之间传递小信息。示例:https://www.mywebsite/central.htm?id=1 稍后在中心页面上,您可以使用JS获取此ID,如下所示:
const url = new URLSearchParams(window.location.search);
const id= url.get('id')
就个人而言,我认为这种方式更容易,更具可读性。
添加回答
举报