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')
就個人而言,我認為這種方式更容易,更具可讀性。
添加回答
舉報