I read a bit about where to put the script src
in the html-file. Some say at the end of the body, some say in the head. The reason is always a performance issue(because the browser will download first the script and then load the page), not that it will not working anymore at all.
But now i have a very strange behavior:
When i put it in the head or at the beginning, the javascript is not working anymore????? When i put it before the end of the body, everything is fine.
This is my html-code:
<!DOCTYPE html>
<html lang="en">
<head>
<script src="doggos.js"></script>
</head>
<body>
<button id="tryIt">Try it</button>
<p id="demo">huhu</p>
</body>
</html>
And this is in the doggos.js:
document.getElementById('tryIt').addEventListener('click', function () {
document.getElementById('demo').innerHTML = 'Hello World';
});
If i put the script-src-phrase
at the end of the body my button works and the huhu transforms to Hello World. If i have the script-src-phrase
in the head or at the beginning of the body the click on the button does not change anything (the JavaScript seems to be not available).
I am a bit confused, because these are very "simple codes" and i don't know where my fault is with the solution to put it in the head, like often recommended.