This question already has an answer here:
- JavaScript: Get image dimensions 7 answers
My question is how do I obtain true image resolution (WxH in pixels) after I change source. The problem is that
var img = document.getElementById('image');
img.src = newURL;
var W = img.naturalWidth;
var H = img.naturalHeight;
doesn't reflect dimensions of replacing image but keeps old values. Later when I inspect the element in debugger, naturalWidth/naturalHeght already adapt new image resolution. Ie both are lazy updated. What I need to know is how to tell JS code, when they're adjusted to new image and it can use the values.