It’s a perfectly dumb question. But I just wanted to clear my doubt. When a image is loading we can check loading state using onload or oncomplete events. But I just wanted to know how much portion of the images is loaded using JavaScript. Is it really possible?
My question is, Can we get image size from URL? and Can we get how much portion of image is loaded in some interval?
If you load the images via an
XMLHttpRequestobject you can bind to theprogressevent and check the function argument “event” properties “loaded” and “total”. As always, support between various browsers might be a surprise.From the linked MDN article:
[Update] Per commenter @Bergi’s question if you want to add the image to the DOM once it has fully downloaded you could add a new image element with:
The “src” attribute equal to the URL you fetched via XHR (and hope that the browser cache will prevent redundant download).
Or set the “src” attribute to a data URI of the Base64 encoded image content and not worry about the browser cache.