EN
JavaScript - get entire document width
5 points
In this article, we're going to have a look at how to get the entire document width with JavaScript.
Presented below solution is based on taking the biggest known width (clientWidth
, html
or body
element) to predict total page size. We assumed that: document, it is all client area with parts that overflow outside.
Note: it is important to check height after
body
element is ready. So checking can be run afterbody
onload
event occured or made in somebody
script
.
Red this article to know how to measture entire document height.
Quick solution:
xxxxxxxxxx
1
2
<html>
3
<body>
4
<script>
5
6
function getEntireWidth() {
7
var html = document.documentElement;
8
var body = document.body;
9
10
var width = Math.max(html.clientWidth,
11
html.scrollWidth, html.offsetWidth,
12
body.scrollWidth, body.offsetWidth);
13
14
return width;
15
}
16
17
function onClick() {
18
var entireWidth = getEntireWidth();
19
console.log('Entire width is equal to ' + entireWidth + '.');
20
}
21
22
</script>
23
<button onclick="onClick()">Measure document width</button>
24
</body>
25
</html>