I'm attempting to familiarise myself with JavaScript and how it interacts with webpages, and so I mocked up a little something below:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example Page</title>
</head>
<body>
<p>This is paragraph 1</p>
<script>
alert("First Script Block")
</script>
<p>This is paragraph 2</p>
<script>
alert("Second Script Block")
</script>
<p>This is paragraph 3</p>
<script>
alert("Third Script Block")
</script>
</body>
</html>
The HTML source defined above contains multiple script tags which display simple alerts. From my understanding HTML is parsed in a top-down fashion, so each script element should be hit after its associated paragraph element. But this does not seem to be the case, instead the script which triggers the alert seems to be encountered before the paragraph content on the page has updated, i.e. I receive the "First Script Block" alert before "This is paragraph 1" is displayed on the page. The intended behaviour I had in mind was for "This is paragraph 1" to be displayed first, and then for the alert "First Script Block" to pop-up.
Could someone explain the shortcoming in my understanding here?