แอตทริบิวต์เหตุการณ์ onload HTML ถูกทริกเกอร์เมื่อมีการโหลดวัตถุในเอกสาร HTML
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์ -
<tagname onload=”script”></tagname>
ให้เรามาดูตัวอย่าง HTML onload event Attribute−
ตัวอย่าง
<!DOCTYPE html>
<html>
<head>
<style>
body {
color: #000;
height: 100vh;
background-color: #FBAB7E;
background-image: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%);
text-align: center;
padding: 20px;
}
</style>
<script>
function get() {
alert("Hi! you just refresh the document.");
}
</script>
</head>
<body onload="get()">
<h1>HTML onload Event Attribute Demo</h1>
<p style="color:#db133a;">Now try to refresh this document.</p>
</body>
</html> ผลลัพธ์

ตอนนี้ให้ลองโหลดเอกสารใหม่เพื่อดูว่าแอตทริบิวต์เหตุการณ์ onload ทำงานอย่างไร
