views:

3743

answers:

4

Hi,

I'm having a document.body is null error in my javascript because I use:

$(window).width()

as value to assign to a variable in my

    $(document).ready(function(){});

I would be very grateful to anyone who could help me with this.

Kind regards

edit: sorry if this all was unclear. I have a demo at: http://www.wpmonk.com/demo/hypowired at first the theme will load but then becomes white (because of the error) but when you reload you can see the whole theme because then he knows the value for $(window).width()

I'm using this code to center the layout (not possible with css because the left needs to have a width aswell.)

function positioneerElement(){      
$breedte = (document.body.clientWidth - 1124) / 2;
$('#bg_left').css({
    'width': $breedte
});
$('.container').css({
    'margin-left': $breedte
});

}

I call function positioneerElement() in the load function.

Sorry if this wasn't clear I though it wasn't necessary to put the demo here for this nor the code. However I would like to thank the people who are trying to help!

+1  A: 

I agree with outis - we need some more code.

That said:

  • Are you sure you're doing all your work inside the $(document).ready function?
  • Do you definitely have a correctly formed (and closed) body tag and valid xhtml?
  • Does this happen on all browsers?

One suggestion:

Does the problem persist if you put the positioneerElement function in the $(document).ready function as well? I'm wondering whether the browser evaluates document.body prior to the document loading.

$(document).ready(function() {
    function positioneerElement(){      
    $breedte = (document.body.clientWidth - 1124) / 2;
    $('#bg_left').css({
        'width': $breedte
    });
    $('.container').css({
        'margin-left': $breedte
    });

    // your other code, including
    positioneerElement();
});
Damovisa
Ayrton
@Ayrton - I've added a suggestion and a code sample
Damovisa
thank you very much. Apperantly it all had to do with this. I found it strange because in other projects I didn't need to put the functions in my document.ready.Cheers
Ayrton
+1  A: 

Well, just based on the information you have given me it might be that the body has not loaded properly yet. Please see here for more detials: http://www.coderanch.com/t/122834/HTML-JavaScript/document-body-null-or-not#606782

I would say more but I too agree with outis that you need to post some more code for us to debug.

Robert Massaioli
+1  A: 

Make sure you only call $(window).width() after you have included the <body> tag (eg. in the ready callback), and/or use a Standards Mode doctype declaration. You should be doing that anyway: Quirks Mode sucks.

The issue is, to read the viewport size in Quirks Mode jQuery must look at document.body.clientWidth instead of document.documentElement.clientWidth. If you haven't started your <body> yet, there is not document.body node present.

bobince
A: 

You have to include jquery.js, and not jquery.min.js It looks like some bug in it This works for me

mpeti