Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the height of the entire webpage?

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?

like image 702
Syed Faizan Avatar asked Dec 16 '16 07:12

Syed Faizan


People also ask

How to find the size of the web page content?

The web page size The web page size consists of the width and height of the page content rendered. Use the following to access the size of the web page content (includes the page's padding, but not border, margin or scrollbars): If pageHeight is bigger than the window inner height, then a vertical scrollbar is displayed. 4. Summary

What's the best way to set the page height and width?

For a responsive full page height, set the body element min-height to 100vh. If you set a page width, choose 100% over 100vw to avoid surprise horizontal scrollbars. I'll leave you with a tutorial from my YouTube channel demonstrating the CSS height and width settings for an HTML page that is full screen size and grows with the content it contains:

How to increase the page size of your website?

The more media on a page, the bigger the page size and the slower it will load. Embedded videos, images, audio, graphics, flash, and other forms of media will increase your page size. First things first, it’s important for the health and performance of your website that you know the size of your website but how to know the total size of a website?

How do I get the size of a page in JavaScript?

Get the Screen, Window, and Webpage Sizes in JavaScript In Web development, to get the size of the screen, window, or web page shown on the screen, we use the width and height properties. The width represents the horizontal axis, and the height represents the vertical axis.


2 Answers

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.

like image 72
Andrii Verbytskyi Avatar answered Sep 17 '22 23:09

Andrii Verbytskyi


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
like image 44
Himanshu Tanwar Avatar answered Sep 18 '22 23:09

Himanshu Tanwar