Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138163

Get true image dimensions in JavaScript after source change [duplicate]

$
0
0

This question already has an answer here:

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.


Viewing all articles
Browse latest Browse all 138163

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>