views:

85

answers:

2

Hi, so I've got this content loader that replaces content within a div with content from a separate page. But the content that arrives contains a menu that uses jQuery and this is not working. Someone told me I need to reinitialize the code. But how do I do that? I've looked into .ajaxComplete(), but I don't really get how I'm supposed to stitch that together with my existing code?

$('.dynload').live('click',
 function(){

 var toLoad = $(this).attr('href')+' #content';
 $('#content').fadeOut('fast',loadContent);
 $('#ajaxloader').fadeIn('normal'); 
 function loadContent() {
     $('#content').load(toLoad,'',showNewContent())
 }
 function showNewContent() {
    $('#content').fadeIn('fast',hideLoader());
    //Cufon.replace('h1, h2, h3, h4, .menuwrapper', { fontFamily: 
'advent'}); 
 }
 function hideLoader() {
     $('#ajaxloader').fadeOut('normal');
 }

 return false;

 });

This is the code I'm using for the jQuery menu:

    $().ready(function() {
                $('#kontrollpanel .slidepanels').kwicks({
                min : 42,
                spacing : 3,
                isVertical : true,
                sticky : true,
                event : 'click'
            });                    
    });

Also, notice how I try to call Cufon as well within the first function? That doesn't really work either, could that be reinitialized as well? Would really appreciate any help at all..

A: 

In your showNewContent() function call this:

       $('#kontrollpanel .slidepanels').kwicks({
            min : 42,
            spacing : 3,
            isVertical : true,
            sticky : true,
            event : 'click'
Francisco Soto
+1  A: 

You can change all your current code to this:

$(function() {
  $('.dynload').live('click', function(){
    $('#ajaxloader').fadeIn('normal');
    var href = this.href + ' #content';
    $('#content').fadeOut('fast',function() {
      $(this).load(href,'', function(data) {
        createMenus();
        $('#ajaxloader').fadeOut('normal');
        $('#content').fadeIn('fast');
        Cufon.replace('h1, h2, h3, h4, .menuwrapper', { fontFamily: 'advent'});
      });
    }); 
    return false;
  });
});
$(createMenus);

function createMenus() {
  $('#kontrollpanel .slidepanels').kwicks({
     min : 42,
     spacing : 3,
     isVertical : true,
     sticky : true,
     event : 'click'
  });                    
}

Currently you have some problems areas here:

$('#content').load(toLoad,'',showNewContent())
//and...
$('#content').fadeIn('fast',hideLoader());

In the above I'm fading out the ajax loader as soon as the content is coming in, but in showNewContent and hideLoader with the () this was likely causing your Cufron error as well). Also I turned your menu creation into a function so it can be re-used, by passing a content. When you do $(selector) what you're actually doing is $(selector, document), document being the default context (update: looking at your actual page, it doesn't matter so simplified the above).

You can pass any context you want, so in your load callback we're now using data as the context which is the content that was just loaded, so it's only looking inside the content your loading for elements to create menus on...not messing with existing menus.

Nick Craver
I'll go ahead and set this as the answer before I've had a chance to try the solution. I have faith that this will fix most of my issues :) Thank you SO much! I'll beginning to understand this alot more now. Will go through the code and try to see exactly what is going on as well, just to make sure I actually understand :)
Kenny Bones
@Kenny - If you have additional questions just ask here..your code is plugin and content dependent so I can't test that thoroughly, just post any issues you have and I'll try to help.
Nick Craver
Ok, initially I see that it doesn't work. Meaning, the content fades out but new content doesn't fade in. You can actually see it in action here:www.matkalenderen.noJust click the blue hyperlink in the middle actually and you'll see what happens.
Kenny Bones
@Kenny - My fault, I was pulling the `href` from the wrong place when I wrapped your code up at the end, give the updated answer a try.
Nick Craver
Yeah, it works now :) Atleast Cufon is working. The slidemenu isn't though. You can try it here: www.matkalenderen.no/index.php. (Nevermind the php errors)There's a menu on the top where it says "Kontrollpanel", this is linked to kontrollpanel.php which you can also access manually. If you access it manually you'll see how the menu in the middle is supposed to behave.
Kenny Bones
@Kenny - Sorry, busy day :) Updated the code above, see if you have any more troubles.
Nick Craver
Thank you :) Everything seems to be working fine. Except for the small delay is still there. I tried removing both Cufon and the calling of the function of the menu. But that doesn't do anything to the problem either actually.
Kenny Bones
@please up Vote yaar
diEcho
Ya, sorry. Accidentally voted it down and now I can't vote it up again until 24 hours.. :(
Kenny Bones