Search code examples
javascriptjqueryheightjquery-load

jQuery $(window).height wrong with dynamic added content


I'm using jQuery to dynamically position my footer, Now the problem is the

$(window).height();

is wrong because i include a navigation using:

$('.navigation').load('includes/navigation.html');

After that I check to see but the $(window).height() doesn't include the height of the added item, I add the item before checking the height so that can't be the problem. I also tried $(document).ready() and $(window).load()

Here's the full code

$(window).load(function(){


    $('.navigation').load('includes/navigation.html');

    var docheight = $(document).height();
    var winheight = $(window).height();

    console.log('window: ' + winheight + ', document: ' + docheight);
});

Solution

  • You should use the load() complete callback to get new document's height, otherwise as load() is async, you would get height before new content is added to the DOM:

    $('.navigation').load('includes/navigation.html', function(){
        var docheight = $(document).height();
    });