frequent
frequent

Reputation: 28503

how to get the height of an element after inserting content via AJAX?

I have a Jquery Mobile page into which I'm loading a form via AJAX. I'm setting the page dimensions using a plugin, which calculates dimensions AFTER the Ajax has run and the layout has been updated (at least the below consoles come in afterwards).

I need to get the height of the element that received the content, but no matter what I try, I'm just getting rubbish ( ~ 169px instead of some 1200px)

Here is my code:

(inside plugin)
some: function(){

    var self = this,
    o = self.options,
    wrap = $('div:jqmData(wrapper="true").ui-page-active').last(),
    // elements
    panels = wrap.find('.ui-panel.ui-panel-active').filter(':not(.ui-popover)'),
    pages = panels.find('.ui-page'),
    contents = pages.find('.ui-content');

    // maxHeight contents
    for ( var i = 0; i < contents.length; i++){         
        ct = contents.eq(i);

        console.log( ct );
        console.log( ct.css("height") )
        console.log( ct.height() );
        console.log( ct.outerHeight() );    
    // max
    if ( ct.css("height") > parseFloat( o._iPadFixHeight ) ){
        o._iPadFixHeight = parseFloat( ct.css("height") ) + parseFloat( ct.css('padding-top')) +  parseFloat( ct.css('padding-bottom')) ;
        };
    }
     ...

If I'm not using any AJAX this works correctly. If I'm adding content dynamically via AJAX, the consoles all fire after the AJAX, but still only return false values.

Question:
How can I reliably get the content element height after an AJAX update? I tried setting a 10sec timeout after Ajax to wait until everything is there. No difference, after 10sec the content element height is still not correct.

Thanks for help!

EDIT:
Although the consoles log AFTER the AJAX load, I think the function is firing before, so it's calcualting based on pre-AJAX dimensions. I'm thinking to re-fire using this:

$(window).on('dimensionchange', function(){             
    self.ome();
    })

And triggering

$(window).trigger('dimensionchange')

in my AJAX success handler. Not perfect, but I guess this will do.

EDIT2:
Got it to work. @HolgerDSchauf was correct. On my first function run, I set the false values and then after AJAX came in, the false value were still set. I'm now fixing it like this:

Ajax Success:

...
// cleanup dimensions
$(window).trigger( 'dimensionclear');
...
window.setTimeout(function() {
   target.removeClass('.fade.in');
   // recalculate
   $(window).trigger('dimensionchange');    
   },250);

In my plugin:

$(window).on('dimensionchange', function(){ 
     self.ome();
     });
$(window).on('dimensionclear', function(){
     self.ome("clear");
     });    

And in OME function:

// cleanup
if ( from == "clear") {
   contents.css({'height':'', 'max-height':''})
   return;
   }

Works like a charm.

Upvotes: 5

Views: 2767

Answers (2)

totten
totten

Reputation: 2817

I use this function to keep all my div's updated sizes. Use something like this, but do not forget to call it when document is ready.

function framing() {
    /*
        Pre         : ready.js, self
        Post            : design.js->framing
        Author          : Enes Ü.
        Date            : 15:30, 05 May 12
        Summary         : framing the page widthly/heightly
        Bugs/TODOs              : Frame heights are anormal and will be fixed. Prior=5
    */


    $("#right_menu").width($("#main").width()-$("#left_menu").width());
    $("#left_menu").height(getWinHeight());
    $("#top_menu").width($("#main").width()-$("#left_menu").width());
    $("#right_menu").height(getWinHeight()-$("#top_menu").height());
    $("#map_container").height(getWinHeight());
    /* 
     ******* lots of lines like above...
     */

    setTimeout(framing,100);
}

Upvotes: 2

Holger D. Schauf
Holger D. Schauf

Reputation: 147

you used an cache element with "wrap, panels, ct" refresh this cached variable or use it instantly

Upvotes: 1

Related Questions