EN
JavaScript - access body element
8 points
In this short article, we would like to show how to get <body>
element reference in JavaScript.
Quick solution:
xxxxxxxxxx
1
var bodyElement = document.body;
Warning:
document.body
can benull
if you access it when body is not ready (e.g. in<head>
element).
xxxxxxxxxx
1
2
<html>
3
<head>
4
<meta charset="utf-8" />
5
<title>Page title</title>
6
</head>
7
<body>
8
<script>
9
10
var bodyElement = document.body; // body element reference
11
12
console.log(bodyElement.outerHTML);
13
14
</script>
15
</body>
16
</html>