Languages
[Edit]
EN

JavaScript - body element creation detecting inside head element (document.body element created event)

2 points
Created by:
Root-ssh
66625

In this article we would like to show how to detect in head element when body element is created using JavaScript.

Simple solution:

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <script>

    window.addEventListener('load', function() {
    	console.log(document.body 
            ? 'Body element created!' 
            : 'Body element not created yet?');
    });
    
	window.addEventListener('DOMContentLoaded', function() {
    	console.log(document.body 
            ? 'Body element created!' 
            : 'Body element not created yet?');
    });
    
    document.addEventListener('DOMContentLoaded', function() {
    	console.log(document.body 
            ? 'Body element created!' 
            : 'Body element not created yet?');
    });

  </script>
</head>
<body>
  <p>Some text ...</p>
</body>
</html>

 

Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join