Menu
Can you explain why the javaScript doesn’t work if JavaScript codes are placed inside <head></head> tag. Why they only work if the codes are placed in body tag?
Thank you.
<!doctype html>
<html>
<head>
<meta charset=”utf-8″>
<title>Untitled Document</title>
</head>
<body>
<p>This example calls a function which returns the value of PI:</p>
<p id=”demo”></p>
<script>
function myFunction (){
return Math.PI;
}
document.getElementById(“demo”).innerHTML = myFunction();
</script>
</body>
</html>