ออบเจ็กต์ HTML DOM popStateEvent เป็นตัวจัดการเหตุการณ์สำหรับเหตุการณ์ popstate ซึ่งเกิดขึ้นเมื่อประวัติของหน้าต่างเปลี่ยนแปลง
คุณสมบัติของ PopStateEvent
| ทรัพย์สิน | คำอธิบาย |
|---|---|
| สถานะ | ส่งคืนวัตถุที่แสดงสำเนาของรายการประวัติ |
ตัวอย่าง
ให้เราดูตัวอย่างของ HTML DOM popStateEvent Object -
<!DOCTYPE html>
<html>
<head>
<style>
html{
height:100%;
}
body{
text-align:center;
color:#fff;
background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) center/cover no-repeat;
height:100%;
}
p{
font-size:1.2rem;
}
.btn{
background:#0197F6;
border:none;
height:2rem;
border-radius:2px;
width:35%;
margin:2rem auto;
display:block;
color:#fff;
outline:none;
cursor:pointer;
}
</style>
</head>
<body>
<h1>DOM PopStateEvent Event Demo</h1>
<button onclick="display()" class="btn">Click Me</button>
<script>
function display(){
window.onpopstate = function(event) {
alert("location: " + document.location +
", state: " + JSON.stringify(event.state));
};
history.pushState({
page: 1
}, "title home", "?page=home");
history.pushState({
page: 2
}, "title about", "?page=about");
history.replaceState({
page: 3
}, "title contact", "?page=contact");
history.back();
history.back();
}
</script>
</body>
</html> ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -

คลิกที่ “คลิกฉัน ” เพื่อทำความเข้าใจว่า PopStateEvent Object ทำงานอย่างไร -

คลิก “ตกลง” –
