Asynchronously load images with jQuery

前端 未结 10 2163
日久生厌
日久生厌 2020-11-22 06:52

I want to load external images on my page asynchronously using jQuery and I have tried the following:

$.ajax({ 
   url: \"http://somedomain.         


        
10条回答
  •  孤独总比滥情好
    2020-11-22 07:10

    $().attr('src','http://somedomain.com/image.jpg');
    

    Should be better than ajax because if its a gallery and you are looping through a list of pics, if the image is already in cache, it wont send another request to server. It will request in the case of jQuery/ajax and return a HTTP 304 (Not modified) and then use original image from cache if its already there. The above method reduces an empty request to server after the first loop of images in the gallery.

提交回复
热议问题