hanif hanif - 1 month ago 7
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">
<head>
<meta charset="UTF-8">
<title> java Script</title>
<link rel="stylesheet" href="style.css">
<script type="text/javascript" src="myjavascript.js"> </script>


</head>

Answer

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.

https://developers.google.com/speed/docs/insights/BlockingJS