1

I do not have much experience at all with Jquery and am having trouble writing a proper callback function so that a separate javascript function executes after having loaded html content through an ajax .load() call.

The following code loads the external html content

$('#myID').find('a').on('click', function(){
    e.preventDefault();
    var $desc = $('#insert');

    switch($(this).attr('href')) {
        case 'content1.html' :
            $desc.load('fragments/content1.html');
            break;
        case 'content2.html' :
            $desc.load('fragments/content2.html');
            break;
    }
});

The following code must then be executed on the html loaded by the previous function

var panelWidth = 0;
var startPanel = 1;

window.panelWidth = $('.sp').width();   


$('.panel_container .hiw_panel').each(function(index){
    $(this).css({'width':window.panelWidth+'px','left':(index*window.panelWidth)});
    $('.sp .panels').css('width',(index+1)*window.panelWidth+'px');
});


$('.switcher span').each(function(){
    $(this).on('click', function(){
        changePanels( $(this).index() );
    });
});


function changePanels(newIndex){

var newPanelPosition = ( window.panelWidth * newIndex ) * -1;
$('.switcher span').removeClass('selected');
}

I've deleted out excess content to make it as concise as possible. All I need to know is how to execute part II after part I. Thanks for your help!

1
  • Use a callback function that isn't anonymous Commented Dec 12, 2013 at 3:08

3 Answers 3

1

Hmm, something like this maybe:

$( '#myID' ).on( 'click', 'a', function( e ) {
    e.preventDefault();

    $( '#insert' ).load( $( this ).attr( 'href' ), function() {
        var panelWidth = 0;
        var startPanel = 1;

        window.panelWidth = $( '.sp' ).width();

        $( '.panel_container .hiw_panel' ).each( function( index ) {
            $( this ).css({
                'width': window.panelWidth,
                'left': ( index * window.panelWidth )
            });

            $( '.sp .panels' ).css({
                'width': ( index + 1 ) * window.panelWidth
            });
        });

        $( '.switcher span' ).each( function() {
            $( this ).bind( 'click', function() {
                changePanels( $( this ).index() );
            });
        });
    });
});

function changePanels( newIndex ) {
    var newPanelPosition = ( window.panelWidth * newIndex ) * -1;
    $( '.switcher span' ).removeClass( 'selected' );
}

What it's doing is binding to the a click, then loading in whatever the href attribute was for the a tag, then doing a callback function which is your step 2 code. (You can read more about .load() here);

I think something might be missing from your changePanels function as the var isn't used, but that's easy to tidy up.

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

1 Comment

Thanks... this seems to be a viable alternative solution as well.
0

Give your function a name - for example executeAfterLoad() - and include it as an argument of .load:

case 'content1.html' :
        $desc.load('fragments/content1.html',executeAfterLoad);
        break;
case 'content2.html' :
        $desc.load('fragments/content2.html',executeAfterLoad);
        break;

1 Comment

Perfect...this worked (as well as AustinAllover's response). Huge help
0

I think you're looking for:

$desc.load('fragments/content1.html', function(){
  //cb code here
});

The second argument is a callback function that is executed when the request completes.

Comments

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.