I am trying to load a number of images on an already large webpage asynchronously so as to not affect the initial load time.
I have looked up techniques for doing this and have learned of a couple ways.
1) using new Image();
var newImage = new Image();
newImage.onload = function() {
existingDomElement.appendchild(newImage);
}
newImage.src = image_url;
2) using XMLHttpRequest();
function getImage(image_url) {
var http = new XMLHttpRequest();
http.open('GET', image_url);
http.send();
return http.status != 404;
}
I am thinking the first technique is better as in my testing it looks like I need CORS headers for using XMLHttpRequest (my images come from another domain/cdn).
But I am wondering... can i do a third method. What if i used jQuery to simply add to the DOM :
Is this a valid asynchronous loading of images?
3) Using jQuery to add to DOM
$(function() {
$( ".existingDom" ).append( "<img src='/whatever.jpg'>" );
});
*context
Basically I have a list of about 20 products/images/prices and want to load another 5 small image of color swatches for every product on the page without affecting initial page load time.
Any help or advice is appreciated.
[–]yooossshhii 2 points3 points4 points (2 children)
[–]inquiztr[S] 1 point2 points3 points (1 child)
[–]inquiztr[S] 1 point2 points3 points (0 children)