I am getting the image size using javascript, but it won’t return the correct size until after I run it a second time. I wonder why this is. Here is my code:
var img = new Image()
img.src = src
img.onLoad = alert(img.width);
This returns 0 sometimes, but after a couple times it returns the actual image width. How do I make it get the right size the first time? I thought this was the right way.
The problem is you are calling alert directly instead of assigning it as a callback so it is called before the image is loaded. This is what you want to do: