แอตทริบิวต์เหตุการณ์ HTML onbeforeprint ถูกทริกเกอร์เมื่อหน้ากำลังจะพิมพ์หรือก่อนที่กล่องโต้ตอบการพิมพ์จะปรากฏในเอกสาร HTML
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์ -
<tagname onbeforeprint=”script”></tagname>
ให้เราดูตัวอย่างของ HTML onbeforeprint 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;
}
p {
font-size: 1.1rem;
}
</style>
</head>
<body onbeforeprint="get()">
<h1>HTML onbeforeprint Event Attribute Demo</h1>
<p>I'm a paragraph HTML element with some dummy text.</p>
<p style="color:#db133a;">Now try to print this document.</p>
<div class="show"></div>
<script>
function get() {
document.body.style.background = "lightblue";
}
</script>
</body>
</html> ผลลัพธ์

ตอนนี้ให้ลองพิมพ์เอกสารโดยใช้ ctrl + p และสังเกตว่าแอตทริบิวต์เหตุการณ์ onbeforeprint ทำงานอย่างไร
