I am currently experiencing a 'strange problem'... I am a beginner in JavaScript and I want to get the height of an image in order to set the size of a .
- I get the width of the screen
- I set the width of an image which depend on the screen size => I guess the computer set also the height of the image.
- I ask for the height of the image
- I print it
This is the code:
var screenWidth = (window.innerWidth);
myImg.width = Math.floor(screenWidth/3); // That works, my image is sized well.
var imgLarg = getComputedStyle(myImg,null).height.toLowerCase()
console.log(imgLarg);
The problem is at follow: When I refresh the page by reloading the page via the web address, it works. But, when I press the refresh button, the code return '0px'.
<img src="waves.jpg" alt="photo" id="imag" />. Juste before</html>, there is the js script as before, with this linevar myImg = document.querySelector('#imag')at the beginning. In a real situation, i want the get the height of the image to set the size a <div>. I have tried withmyImg.onload = function(){}but i experience difficulties...