How to use JavaScript onload function to display a message

Viewed 498

I am approaching for the first time in JavaScript and I am trying to implement the onload function to show a message every time the page of my site is loaded. Can you help me by showing me some lines of code?

1 Answers

Never use body onload. It is very simple for other scripts to overwrite.

Instead use addEventListener which will ADD your function to the load and cannot be overwritten

<html>
<header>
  <meta charset="UTF-8">
</header>
<script>
  window.addEventListener("load", function() {
    alert("Hey!");
  })
</script>

<body>
</body>

</html>

That said, it is very weird that an alert was dismissed since it is system modal. Are you sure you use alert?

Related