-1

I have a "very simple" problem. My code is:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    Hello World!
    <script>
        alert('Alert');
    </script>
</body>
</html>

First it executes alert(), only then makes HTML the "Hello World!" string to visible. How can I make to appear the Hello World first, and only then execute alert('Alert')? Thanks forward!

  • 1
    Possible duplicate of [when is window.onload fired](https://stackoverflow.com/questions/3520780/when-is-window-onload-fired) – Alon Eitan Nov 03 '18 at 12:36

1 Answers1

3

Use window.onload. From docs,

The load event fires at the end of the document loading process. At this point, all of the objects in the document are in the DOM, and all the images, scripts, links and sub-frames have finished loading.

window.onload = () => alert('Alert');
Hello World!
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59