+ Reply to Thread
Results 1 to 7 of 7

Thread: Jquery: document.body is null?

  1. #1
    Teensweb is offline x10 Lieutenant Teensweb is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    352

    Jquery: document.body is null?

    Hi everyone,
    I have been really annoyed by this irritating bug with internet explorer and jquery.
    So here's the story:
    I have an html page with jquery (v 1.5) on it and as usual, everything works like charm in heavenly browsers like ff and chrome. But in IE, the problem is really strange! The page reports error onload and when I view the error window it says "document.body is null or not an object"
    and some of my jquery pluggins fail to work correctly, furthermore, the error disappears when I refresh the page! I googled about this and found that similar bugs where reported by others but I could never actually find a solution
    Here's my html for reference:
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <link rel="Stylesheet" type="text/css" media="screen" href="Styles/Template.css">
    <link rel="Stylesheet" type="text/css" media="screen" href="Styles/pluggins.css">
      <script type="text/javascript" src="inc/jquery.js"></script>
          <script type="text/javascript" src="inc/jquery.metadata.js"></script>
        <script type="text/javascript" src="inc/jquery-ui.min.js"></script>
      <script type="text/javascript" src="inc/lavalamp.js"></script>
      <script type="text/javascript">
    
    $(function(){
          
          // some function
        });
      </script>
    
    <title></title>
    </head>
    <body>
    <div id="layout">
    <div id="leftpane">
    <div class="panetop"></div>
    <div class="ext">
      </div>
    <div class="panefoot"></div>
    </div>
    <div id="rightpane"style="right:0px">
    <div class="panetop"></div>
    <div class="ext">
      </div>
    </div>
    <div class="panefoot"></div>
    </div>
    <br>
    <br>
    </div>
    </body>
    </html>
    Last edited by Teensweb; 02-22-2011 at 05:49 AM.

  2. #2
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: Jquery: document.body is null?

    If you are executing the Javascript straight away in the "$(function()..." bit then the body tag may or may not exists, depending upon how far downloaded the page is. This is what is causing it to sometimes work and sometimes not, as loading it from the cache will mean the body element exists, but re-downloading the page may cause the code to execute before the body exists.

  3. #3
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Jquery: document.body is null?

    $(function () {...}) is the same as $(document).ready(function () {...}), so the function won't execute until the DOM is ready.

    The sample page (even when filled out) doesn't recreate the problem in IE8 (whether running in IE7, 8 or compatibility mode, or quirks or standards mode), and has extraneous elements. Please post a minimal sample case, properly indented.

    For example, the following doesn't produce any error.
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
      <head>
        <script type="text/javascript" src="/script/jquery.js"></script>
        <script type="text/javascript">
        $(function(){
            document.body.appendChild(document.createTextNode('sample text'));
        });
        </script>
        <title></title>
      </head>
      <body>
      </body>
    </html>
    Last edited by misson; 02-22-2011 at 04:14 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  4. #4
    Teensweb is offline x10 Lieutenant Teensweb is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    352

    Re: Jquery: document.body is null?

    Alright, I short-listed the problem to one of my pluggins: mbcontainers. It happens only when I include this particular file and it happens even if I don't call any functions in the file. Again, the error vanishes when I refresh the page!
    One more interesting news: it happens only when the mbContainer.js file is fetched from a local directory!
    Here's my sample code ( Thanks for the guidelines, misson)
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
      <script type="text/javascript" src="inc/jquery.js"></script>
          <script type="text/javascript" src="inc/jquery.metadata.js"></script>
        <script type="text/javascript" src="inc/jquery-ui.min.js"></script>
      <script type="text/javascript" src="inc/mbContainer.js"></script>
      <script type="text/javascript">
    
    $(function(){
          
          // some function
        });
      </script>
    </head>
    <body>
    </body>
    </html>
    The above code gives me the said error in IE and works fine in ff and chrome. And furthermore, it now seems that the error vanishes even if jquery is fetched remotely instead of mbcontainers!
    Repeat: The mbContainer.js should be fetched locally for the error to appear.!
    I'm really annoyed by this bug just because of its nature. The error consoles in ff and chrome does not even give a warning and I can't seem to interpret anything out of IE error reports to even know where to start debugging, So I would greatly appreciate any help, thanx.
    Last edited by Teensweb; 02-23-2011 at 05:21 AM.

  5. #5
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Jquery: document.body is null?

    Quote Originally Posted by Teensweb View Post
    it happens only when the mbContainer.js file is fetched from a local directory!
    Do you mean you're opening the HTML document as a local file, rather than via HTTP? What IE version are you testing?

    The error still isn't reproducible. After saving the sample HTML to a file and downloading jQuery, mbContainerPlus and the necessary jQuery UI components to the appropriate folder and opening the page locally in IE8, there was no error. To make a self-contained example in this case, you'll need to create an archive of all the files, or post a link to a live page so it can be downloaded.
    Last edited by misson; 02-25-2011 at 03:13 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  6. #6
    Teensweb is offline x10 Lieutenant Teensweb is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    352

    Re: Jquery: document.body is null?

    Yes, you got me right. But this is weird, what you did to reproduce the error exactly matches the situation that causes the error for me. I'm on IE8 too, under win7. I don't think this is gonna affect my website anyway as the error vanishes when the files are uploaded to an http server. But this really gives me the crap while trying to understand javascript and building cross-browser compatible designs... Are you on win 7 too?

  7. #7
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Jquery: document.body is null?

    I tested on Vista SP2, 32 bit.

    The best thing to try is debugging the script with the IE developer tools.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Null Script Suspension
    By shriper in forum Free Hosting
    Replies: 11
    Last Post: 06-26-2010, 08:54 AM
  2. Null?
    By halohub2 in forum Free Hosting
    Replies: 1
    Last Post: 01-30-2010, 04:21 AM
  3. Null Script
    By steve-qdogg in forum Free Hosting
    Replies: 2
    Last Post: 01-13-2010, 12:41 AM
  4. Null cPanel
    By cstory in forum Free Hosting
    Replies: 2
    Last Post: 10-10-2009, 09:32 AM
  5. Fix IPB Null Versions
    By Shadow121 in forum Scripts & 3rd Party Apps
    Replies: 5
    Last Post: 12-10-2006, 03:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers