1

This line does not work

$('#bookcontainer').load( startSlide );

while this line does

$(window).load( startSlide );

#bookcontainer is a div containing four images.

Here's my whole code:

// JavaScript Document
$('#bookcontainer').load( startSlide );

var slide;

$('#slider').hover( 
function() {
        $('.arrow').show();
}, 
function() {
        $('.arrow').hide();
});

function startSlide() {
    $('.book').show();
    slide = setInterval(slideR, 5000);   
}


function slideR() {
    $('.book').first().css('left', '960px').appendTo('#bookcontainer').animate(
    {
        "left": "-=960px"
    }, {
        duration: 1000,
        easing: 'easeOutCubic'
    });

}


function slideL() {
    $('.book').last().animate(
    { "left":"+=960px" }, {
        duration: 1000,
        easing: 'easeOutCubic',
        complete: function() {
        $(this).prependTo('#bookcontainer').css('left', '0px');   
        }
    });

}


function right() {
    clearInterval(slide);
    slideR();
    startSlide();
};


function left() {
    clearInterval(slide);
    slideL();
    startSlide();
};

3 Answers 3

6

The load event can only be handled on elements that are associated with a url. From the documentation:

The load event is sent to an element when it and all sub-elements have been completely loaded. This event can be sent to any element associated with a URL: images, scripts, frames, iframes, and the window object.

If you're waiting on all of your images to load, you could apply the event handler to those images individually.

Also: $(window).load(...) will not be fired until all elements on the page are fully loaded (including graphics), so it's not necessary to bind the event to a div to make sure child elements have loaded.

If you're making an AJAX request and attempting to detect when content has been loaded into that div, you should make startSlide the success callback to your AJAX request.

Sign up to request clarification or add additional context in comments.

5 Comments

how do I call a function once a div containing images (hence all the images) is loaded?
@user643440: If you use $(window).load(...), all images inside that div (and all other divs) will have loaded.
I gave the last image in the div an ID and called $('#lastbook').load(startSlide); Start slide will not go until all the images are loaded, pretty sure.
cannot use $(window).load(...) because a different script is using it
@user643440: You can bind multiple event handlers to $(window).load(...) and it won't cause issues.
0

You should use $('#bookcontainer').load('url', startSlide ); have a look at http://api.jquery.com/load/

Comments

-1

The div element does not trigger a load event.

2 Comments

load is not an event but jQuery's shorthand method to load contents through Ajax call
@jatanp: Not true: load is an event and its only triggered on certain elements, see my answer for more information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.