Languages
[Edit]
EN

JavaScript - why document.body is null?

9 points
Created by:
Root-ssh
179580

In this article, we would like to show you how to solve document.body is null problem in JavaScript.

Overview

Sometimes in browser JavaScript document.body can be null. It is caused because body element is not ready yet. It happens when the programmer executes source code inside head element. In this article simple solutions how to solve this problem have been described.

Example source code with issue:

// ONLINE-RUNNER:browser;

<!doctype>
<html>
<head>
  <script>
    
    function onLoad() {
        if (document.body) {
    	    console.log('document.body exists');
        } else {
            console.log('document.body does not exist');
        }
    }

    onLoad();
    
  </script>
</head>
<body>
  Web page content...
</body>
</html>

Problem solutions have been presented below.

1. onload event for body tag example

// ONLINE-RUNNER:browser;

<!doctype>
<html>
<head>
  <script>
    
    function onLoad() {
        if (document.body) {
    	    console.log('document.body exists');
        } else {
            console.log('document.body does not exist');
        }
    }
    
  </script>
</head>
<body onload="onLoad()">
  Web page content...
</body>
</html>

2. onload event for window object example

// ONLINE-RUNNER:browser;

<!doctype>
<html>
<head>
  <script>
    
    function onLoad() {
        if (document.body) {
    	    console.log('document.body exists');
        } else {
            console.log('document.body does not exist');
        }
    }
    
    window.onload = onLoad;
    // or:
    // window.addEventListener('load', onLoad);
    
  </script>
</head>
<body>
  Web page content...
</body>
</html>

3. DOMContentLoaded event for document object example

// ONLINE-RUNNER:browser;

<!doctype>
<html>
<head>
  <script>
    
    function onLoad() {
        if (document.body) {
    	    console.log('document.body exists');
        } else {
            console.log('document.body does not exist');
        }
    }
    
    document.addEventListener('DOMContentLoaded', onLoad);
    
  </script>
</head>
<body>
  Web page content...
</body>
</html>

4. JavaScript logic execution after expected elements are loaded example

// ONLINE-RUNNER:browser;

<!doctype>
<html>
<head>
  <script>
    
    function onLoad() {
        if (document.body) {
    	    console.log('document.body exists');
        } else {
            console.log('document.body does not exist');
        }
    }

  </script>
</head>
<body>
  Web page content...
  <script>
    
    onLoad();
    
  </script>
</body>
</html>

Note: the best way is to execute source source at the end of body element what has been shown in this example.

5. JavaScript logic execution after body element example

// ONLINE-RUNNER:browser;

<!doctype>
<html>
<head>
  <script>
    
    function onLoad() {
        if (document.body) {
    	    console.log('document.body exists');
        } else {
            console.log('document.body does not exist');
        }
    }

  </script>
</head>
<body>
  Web page content...
</body>
<script>
    
    onLoad();
    
</script>
</html>

6. jQuery ready method example 1

// ONLINE-RUNNER:browser;

<!doctype>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script>

    // on ready can be placed inside body element too
    $(document).ready(function() {
        if (document.body) {
    	    console.log('document.body exists');
        } else {
            console.log('document.body does not exist');
        }
    });

  </script>
</head>
<body>
  Web page content...
</body>
</html>

7. jQuery ready method example 2

// ONLINE-RUNNER:browser;

<!doctype>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script>

    // on ready can be placed inside body element too
    $(function() {
        if (document.body) {
    	    console.log('document.body exists');
        } else {
            console.log('document.body does not exist');
        }
    });

  </script>
</head>
<body>
  Web page content...
</body>
</html>
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join