How to find the height of the entire webpage?
Asked Answered
S

7

7

I am working on a solution capturing screen-shots of websites. I am using the default example mentioned at slimerjs.org to get the job done.

Screen-shots are great with this tool, but I need to take full height screen-shots of websites. When capturing screens of websites like http://www.yellowpages.com, I can get full length screens without having to mention any height parameter.

But when I try this url: http://votingbecause.usatoday.com/

I only get screenshot of the set resolution (default: 1920x1080), I can't get full length images.

Since I am using slimerjs, I can manipulate the dom by using jQuery. I need to find complete website height so that I can take screenshot of that resolution

I tried

  • document.height()
  • document.body.scrollheight
  • screen.height
  • $(document).height()
  • (and many other which i found)

But all of these only gave the height of the viewport (website in view)

Question is, how to find the full scrollable height of the website?

Stinkwood answered 16/12, 2016 at 7:26 Comment(2)
To provide a good question, you should put relevant source code so far and all information directly into the the question. When a linked site goes down, the question becomes worthless.Galata
i apologize for this, but the main question was to get the height of the website, i guess i shouldn't have mentioned why i needed the height parameterStinkwood
U
5

To be more general and find the height of any page you could just find the highest DOM Node on current page with a simple recursion:

;(function() {
    var pageHeight = 0;

    function findHighestNode(nodesList) {
        for (var i = nodesList.length - 1; i >= 0; i--) {
            if (nodesList[i].scrollHeight && nodesList[i].clientHeight) {
                var elHeight = Math.max(nodesList[i].scrollHeight, nodesList[i].clientHeight);
                pageHeight = Math.max(elHeight, pageHeight);
            }
            if (nodesList[i].childNodes.length) findHighestNode(nodesList[i].childNodes);
        }
    }

    findHighestNode(document.documentElement.childNodes);

    // The entire page height is found
    console.log('Page height is', pageHeight);
})();

You can test it on your sample site(http://votingbecause.usatoday.com/) with pasting this script to a DevTools Console.

Enjoy!

P.S. Supports iframe content.

Ultramicroscopic answered 16/12, 2016 at 9:17 Comment(1)
just a doubt, does this work with websites using iframes?Stinkwood
S
7

Updated Answer for 2020:

You can just simply pass this below line to get the Height of the Webpage

Code:

console.log("Page height:",document.body.scrollHeight);
Scarabaeoid answered 17/1, 2020 at 6:42 Comment(0)
U
5

To be more general and find the height of any page you could just find the highest DOM Node on current page with a simple recursion:

;(function() {
    var pageHeight = 0;

    function findHighestNode(nodesList) {
        for (var i = nodesList.length - 1; i >= 0; i--) {
            if (nodesList[i].scrollHeight && nodesList[i].clientHeight) {
                var elHeight = Math.max(nodesList[i].scrollHeight, nodesList[i].clientHeight);
                pageHeight = Math.max(elHeight, pageHeight);
            }
            if (nodesList[i].childNodes.length) findHighestNode(nodesList[i].childNodes);
        }
    }

    findHighestNode(document.documentElement.childNodes);

    // The entire page height is found
    console.log('Page height is', pageHeight);
})();

You can test it on your sample site(http://votingbecause.usatoday.com/) with pasting this script to a DevTools Console.

Enjoy!

P.S. Supports iframe content.

Ultramicroscopic answered 16/12, 2016 at 9:17 Comment(1)
just a doubt, does this work with websites using iframes?Stinkwood
B
3

The contents in the site are in the following div

<div class="site-wrapper flex column">

use this code to get it's height

document.querySelector(".site-wrapper").scrollHeight
Bongbongo answered 16/12, 2016 at 7:41 Comment(2)
this is great, but is there a generic solution to finding the height , so it works for other websites too?Stinkwood
As far as I know there isn't any direct way of achieving it. Every website will have different container for their content and they could be nested within other elements like in the site you gave.Bongbongo
H
1

try windows.innerHeight to get browser viewport size i think that is what you are loking for

Hahnemann answered 28/6, 2021 at 14:50 Comment(0)
L
0
$("body").height();

is what you need

Lowgrade answered 16/12, 2016 at 7:29 Comment(4)
i tried this, i am only getting the height of the website in view, the height of the above website should be nearly 5000 pxStinkwood
the second page is 1536px heigh not 5000px.Acinus
@Acinus how did u get that value, please tell :D when i set 5000 px in the tool i made, i get the full website screenshot,so i assumed it was 5000pxStinkwood
i opened "inspect element" and clicked on the body tag. in the left bottom corner it said the dimensions for me. (opera browser)Acinus
T
0
$(window).height();  

retrieve current window height..

Tbilisi answered 16/12, 2016 at 7:39 Comment(0)
K
0

Here is what I found that worked. document.body.scrollHeight was giving me a value of 0. What worked was

document.documentElement.scrollHeight

https://minimul.com/getting-a-zero-with-document-body-scrollheight.html

Khz answered 7/6, 2021 at 15:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.