daniel.tosaba daniel.tosaba - 7 months ago 30
HTML Question

jquery load() after ajax page is loaded - howto

i have following lines included at the bottom of my index.php:

<script type="text/javascript" language="javascript" src="class/jquery-1.4.3.min.js"> </script>
<script type="text/javascript" language="javascript" src="class/jquery.nivo.slider.pack.js"></script>
<script type='text/javascript' language='javascript'>
$(window).load(function() {
$('#slider').nivoSlider();
});
</script>


problem lies in
$(window).load
...

index.php's body is updated onload through ajax call which delivers div#slider upon matching. this makes it
nivoSlider()
not executing. do you have any trick to make this thing work. i do prefer non-jquery way around it, but at the end of the day anything helps.

many thanks

WEBPAGE IS HERE

Answer

Add the call in the callback for the AJAX load.

$('.something').load( 'http://www.example.com/foo', function() {
     $(this).find('#slider').nivoSlider();
});

Example using your code (for body):

$(function() { // run on document ready, not window load
     $('#content').load( 'page.php?c=2&limit=5', function() {
           $(this).find('#slider').nivoSlider();
     });
});

For link:

<!-- updates links like so -->
<a class='nav' href='page.php?category=art&limit=5&c=1'>art</a>

// in the same document ready function
     $('a.nav').click( function() {
          var $link = $(this);
          $('#content').load( $link.attr('href'), function() {
               $(this).find('#slider').nivoSlider();
               $link.addClass('selected'); // instead of setting bg directly to #828282;
          });
          return false; // to prevent normal link behavior
     });