hanif hanif - 14 days ago 5x
HTML Question

js external is not working in head tag of html

My js external file is not working in a head tag, however, it's working on calling at the end of code.

<!doctype html>
<html lang="en">
<meta charset="UTF-8">
<title> java Script</title>
<link rel="stylesheet" href="style.css">
<script type="text/javascript" src="myjavascript.js"> </script>



Before the browser can render a page it has to build the DOM tree by parsing the HTML markup. During this process, whenever the parser encounters a script it has to stop and execute it before it can continue parsing the HTML. If your script tries to access an element in the <body> of your file, it will fail because that part of the DOM is not loaded as yet.

Place your script before the end <body> tag.