Asynchronously load images with jQuery

前端 未结 10 2173
日久生厌
日久生厌 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:00

    $(function () {

        if ($('#hdnFromGLMS')[0].value == 'MB9262') {
            $('.clr').append('<img src="~/Images/CDAB_london.jpg">');
        }
        else
        {
            $('.clr').css("display", "none");
            $('#imgIreland').css("display", "block");
            $('.clrIrland').append('<img src="~/Images/Ireland-v1.jpg">');
        }
    });
    
    0 讨论(0)
  • 2020-11-22 07:07

    You can use a Deferred objects for ASYNC loading.

    function load_img_async(source) {
        return $.Deferred (function (task) {
            var image = new Image();
            image.onload = function () {task.resolve(image);}
            image.onerror = function () {task.reject();}
            image.src=source;
        }).promise();
    }
    
    $.when(load_img_async(IMAGE_URL)).done(function (image) {
        $(#id).empty().append(image);
    });
    

    Please pay attention: image.onload must be before image.src to prevent problems with cache.

    0 讨论(0)
  • 2020-11-22 07:08

    This works too ..

    var image = new Image();
    image.src = 'image url';
    image.onload = function(e){
      // functionalities on load
    }
    $("#img-container").append(image);
    
    0 讨论(0)
  • 2020-11-22 07:08

    AFAIK you would have to do a .load() function here as apposed to the .ajax(), but you could use jQuery setTimeout to keep it live (ish)

    <script>
     $(document).ready(function() {
     $.ajaxSetup({
        cache: false
    });
     $("#placeholder").load("PATH TO IMAGE");
       var refreshId = setInterval(function() {
          $("#placeholder").load("PATH TO IMAGE");
       }, 500);
    });
    </script>
    
    0 讨论(0)
  • 2020-11-22 07:09

    Using jQuery you may simply change the "src" attribute to "data-src". The image won't be loaded. But the location is stored with the tag. Which I like.

    <img class="loadlater" data-src="path/to/image.ext"/>
    

    A Simple piece of jQuery copies data-src to src, which will start loading the image when you need it. In my case when the page has finished loading.

    $(document).ready(function(){
        $(".loadlater").each(function(index, element){
            $(element).attr("src", $(element).attr("data-src"));
        });
    });
    

    I bet the jQuery code could be abbreviated, but it is understandable this way.

    0 讨论(0)
  • 2020-11-22 07:10
    $(<img />).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.

    0 讨论(0)
提交回复
热议问题