views:

39

answers:

4

I have a problem with this code...
The preloader shows up when this function is fired, but it never hides.
The page also shows up before the image inside it is finished loading.

Is there something wrong with my load function?

$('select.select').change(function() {

    var x = $(this).val();

    //Hide the current page and show the preloader
    $('#page' + x).hide();
    $('#pageLoader *').show();

    //when current page is loaded, hide preloader and show page
    $('#page' + x + ' img').load(function() {
      $('#page' + x).show();
      $('#pageLoader *').hide();
    });

});
A: 

Post your html as well...

Or just try it on http://jsfiddle.net/ ;-)

Yves M.
Since it's not a solution, this should be a comment.
Andy E
+1  A: 

If the <img> element is being created after the event handler is set you will need to bind the handler using live() instead, which will bind to existing and future elements matching the selector on the page:

$('#page' + x + ' img').live("load", function() {
  $('#page' + x).show();
  $('#pageLoader *').hide();
});

http://api.jquery.com/live/

Andy E
A: 

You need to provide a url as the first parameter of the load() function.

See documentation.

William
There are 2 load methods, he wants the image's `load` event handler :)
Nick Craver
ar, of course :D
William
+1  A: 

If the <img> already has a src attribute you need to loop through and check in the case it comes from cache, like this:

$('#page' + x + ' img').one('load', function() {
  $('#page' + x).show();
  $('#pageLoader *').hide();
}).each(function() { 
  if(this.complete) $(this).load();
});
Nick Craver
+1 That's pretty slick. Is `this.complete` just for images?
patrick dw
It doesn't seem to be working the same thing is happening
cat
@patrick - yup, sure is
Nick Craver
@cat - another issue is the browser may not be loading it if it's hidden...which browser are you in?
Nick Craver
Im using the safari browser on the ipad. I used a .load function like the one i posted in my question in my $(window).load function and it works perfectly, But that only works on the first image when you first load the page.Now im trying to make it work for my other images on my page but that function doesn't work for some reason.
cat
I am also checking it on firefox on the computer, but more so on the ipad safari browser.
cat