Add an event listener to the window object and call alert
within the event handler, when the load
event occurs:
window.addEventListener('load', function() {
alert('Helo!');
})
<!DOCTYPE html>
<html>
<head>
<title>JavaScript</title>
</head>
<body>
<h1>JavaScript in HTML</h1>
<script>
window.addEventListener('load', function () {
alert('Helo!');
})
</script>
</body>
</html>
Or add an event listener to the document and use setTimeout
with a 0
delay to defer the execution of alert
within the DOMContentLoaded
event handler:
document.addEventListener('DOMContentLoaded', function() {
setTimeout(function() {
alert('Helo!');
}, 0);
});
<!DOCTYPE html>
<html>
<head>
<title>JavaScript</title>
</head>
<body>
<h1>JavaScript in HTML</h1>
<script>
document.addEventListener('DOMContentLoaded', function() {
setTimeout(function() {
alert('Helo!');
}, 0);
});
</script>
</body>
</html>