หากคุณต้องการทริกเกอร์เหตุการณ์ก่อนที่จะโหลดเอกสาร ให้ใช้ onbeforeunload เหตุการณ์
ตัวอย่าง
คุณสามารถลองเรียกใช้โค้ดต่อไปนี้เพื่อเรียนรู้วิธีใช้งาน onbeforeunload event ใน JavaScript
<!DOCTYPE html>
<html>
<body onbeforeunload="return myFunction()">
<a href="https://www.qries.com">Click to open Qries</a>
<script>
function myFunction() {
return "Working with onbeforeunload event";
}
</script>
</body>
</html>