So, I am writing javascript that replaces the default image, which fills the space for numerous images on my web page. Thus, the page loads much faster. (the script is displayed with the body loading), all images by default have the same class, and their identifier is equal to their file name.
function imgPostLoad(totalpics, placeholder) {
var img = document.createElement('img');
for (var i = 0; i < totalpics; i++) {
var picture = document.getElementsByClassName(placeholder)[i];
img.onload = function (evt) {
picture.src = this.src;
picture.width = this.width;
picture.height = this.height;
}
img.src = "/img/" + picture.getAttribute("id") + ".jpg";
}
}
It works, but only for the last last image in the array. The remaining images remain unchanged. What is wrong with him?
source
share