แอตทริบิวต์เหตุการณ์ onbeforeunload จะทำงานเมื่อเอกสารพร้อมที่จะถูกโหลดออก (unload)
ตัวอย่าง
ลองรันโค้ดด้านล่างเพื่อใช้งานแอตทริบิวต์ onbeforeunload:
<!DOCTYPE html>
<html>
<body onbeforeunload="return display()">
<p>ปิดหน้าต่างนี้หรือกด F5</p>
<script>
function display() {
return "Wanna stay here or leave?";
}
</script>
</body>
</html>