views:

88

answers:

2

I am developing my own lightbox kind of jquery plugin. Everything works but I want to hide the loaded content until the images have loaded in the browser from the AJAX call. I found a similar post and I am using the following script but the setTimeout function is what reveals the content and not the .load function. Am I trying to achieve the impossible?

$.ajax({
      url: 'meet/'+ pLoad + '.html',  
      success: function(data) {
        var imageCount = $(data).filter('img').length;
        var imagesLoaded = 0;
        $(data).hide()
         .appendTo('#zoom_inner')
         .filter('img')
         .load( function() {
          ++imagesLoaded;
          if (imagesLoaded >= imageCount) {
          $('#zoom_inner').children().show();
          }
          });
        setTimeout( function() { $('#zoom_inner').children().show() }, 5000 );
       }
    });
A: 

If you investigate this article I think you can find solution for your problem (http://jqueryfordesigners.com/image-loading/)

Best wishes.

Maksim Kondratyuk
+1  A: 

Regarding your comments:

data is just a string in your success callback - it's "html", but it's a string.

make it a node to use:

var $images = $(data); // i.e. turn <div><img /></div> into a div with an img as a child or whatever you got
var imageCount = $images.find('img').length;

cool, eh?

Dan Heberden
http://dev.alcostores.com/abt_career_meet.aspx
Ben4Himv
Have you tested any variables? is imageCount getting the right count? is imagesLoaded increasing? even `alert(imagesLoaded);` after the `++imagesLoaded;` would be helpful to verify.
Dan Heberden
doing that now and imageCount isn't incrementing past 0Troubleshooting that now. Any ideas why that wouldn't $(data).filter('img').length; would only return 0
Ben4Himv
see revised answer
Dan Heberden
THanks Dan, I arrived at a similar solution - $(data).hide().appendTo('#zoom_inner'); var images = document.getElementById("zoom_inner").getElementsByTagName('img');
Ben4Himv
Could be rewritten as `$('#zoom_inner').append( $(data).hide() ).find('img');` for the sake of jQuery coolness :p
Dan Heberden