I was making a webpage with a video. and I wanted to change the size of the video depending on the size of the window.
the code below is my HTML code:
<video id="video" controls="controls" poster="images/video_poster.png" width="320" height="240">
<source src="video/Minions.mp4" type="video/mp4">
<source src="video/Minions.ogv" type="video/ogg">
</video>
and my script code for that is below:
<script>var video = document.getElementById("video");
function changeVideoSize() {
console.log("width is: ", window.innerWidth)
if (window.innerWidth > 900) {
video.width = "640";
video.height = "480";
}
else {
video.width = "320";
video.height = "240";
}
}
window.addEventListener("resize", changeVideoSize)
function init() {
}
init();
</script>
first, I place this script code at the end of the head tag, and it made an error "Cannot set properties of null (setting 'width')".
I was stuck with this error for an hour... and then after many different tries, I put it at the end of body tag, and it worked.
Why is it? What are the main differences between putting it in head and body?? Should I always put the script tag at the end of the body tag? or it depends on the cases?