When i declare script tag in head
tag my code doesn't work, example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="jquery-1.11.2.js" async></script>
<script type="text/javascript" src="skript.js" async></script>
</head>
<body>
<p>
Click "Try it" to execute the displayDate() function.
</p>
<button id="myBtn">
Try it
</button>
<p id="demo"></p>
</body>
</html>
but when i set the script tag in the bottom body
tag it works, example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<p>
Click "Try it" to execute the displayDate() function.
</p>
<button id="myBtn">
Try it
</button>
<p id="demo"></p>
<script type="text/javascript" src="jquery-1.11.2.js" async></script>
<script type="text/javascript" src="skript.js" async></script>
</body>
</html>
skript file is containing the following:
document.getElementById("myBtn").onclick = function(){displayDate()};
function displayDate() {
document.getElementById("demo").innerHTML = Date();
}
does anyone have a precise answer why is this happening?