I have really strange problem. I’m trying to get width and height of an image using following way:
- Create a image tag and add .src to it.
- add this image tag to document.body.
- making this image tag visible (display:inline).
- getting .offsetWidth and .offsetHeight of element.
- hidding image tag(display:none);
all this is done using JS and it all work really well on my localhost, but when I uploaded a site to my client hosting provider I was amazed. offsetWidth and .offsetHeight was 0 in step 4. Why do that happen? I think that I need some kind of “flush” after step 3 before step 4, but I’m not exactly sure. Any suggestions ? Thank you.
You have to wait for the image to actually load over the network. You don’t need to add the thing to the DOM either:
Here, “handleImageSize” would be a function you write to do whatever it is you need to do with the image dimensions.