EN
JavaScript - body element creation detecting inside head element (document.body element created event)
2 points
In this article we would like to show how to detect in head
element when body
element is created using JavaScript.
Simple solution:
xxxxxxxxxx
1
2
<html>
3
<head>
4
<script>
5
6
window.addEventListener('load', function() {
7
console.log(document.body
8
? 'Body element created!'
9
: 'Body element not created yet?');
10
});
11
12
window.addEventListener('DOMContentLoaded', function() {
13
console.log(document.body
14
? 'Body element created!'
15
: 'Body element not created yet?');
16
});
17
18
document.addEventListener('DOMContentLoaded', function() {
19
console.log(document.body
20
? 'Body element created!'
21
: 'Body element not created yet?');
22
});
23
24
</script>
25
</head>
26
<body>
27
<p>Some text ...</p>
28
</body>
29
</html>