Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Is there any accurate (or semi-accurate) way to take a URL and determine the height of the page in pixels?

I have an ad on my site and on my larger pages it looks weird so I want to calculate the page height and determine how many ads I can place on top of each other.

Replies are listed 'Best First'.
Re: Determining page height
by pg (Canon) on Nov 13, 2005 at 03:35 UTC

    Let's simplified the situation, say that the page only contains text. Change the question to how many lines will the page occupy when it is displayed. Even with this largely simplified question, you still cannot have a definitive answer, as the paragraphes can be wrapped around differently because of: 1) the font; 2) the resolution of the screen etc.

    On the other hand, even if there is an answer to this question, a page with excessive advertisements on it, is still very unpleasant. You probably should really reduce the number of ads you have on each page, and not waste time to calculate the height.

Re: Determining page height
by Aristotle (Chancellor) on Nov 13, 2005 at 03:24 UTC

    No, you can’t.

    You can probably use something like Gtk2::MozEmbed, Gtk2::Html2 or Win32::InternetExplorer::Window to render the page and then ask the window for its height.

    But even then, what is the height? If the browser’s viewport is 600 pixel wide, the page will almost certainly be higher than if it’s 1360 pixel wide.

    Web pages aren’t PDFs. They just don’t have a height.

    Makeshifts last the longest.

Re: Determining page height
by spiritway (Vicar) on Nov 13, 2005 at 05:55 UTC

    Unfortunately, not really... One problem with HTML is that it was originally designed for the display of information. The visual considerations of the display were of secondary importance - what mattered was how each data element was characterized. That made it easier to make tables of contents, indexes, and cross-references. It left the actual display to the rendering engine.

    The upshot now is that while certain elements may have similar appearances in different browsers, there really is not a reliable way to determine the exact appearance of the elements.

    Compounding this is the wide variety of screen sizes, resolutions, fonts, user preferences, etc., that make your efforts essentially impossible. While you *can* sometimes rigidly specify certain sizes, it is usually not a good idea because someone out there is using a screen that is too big or small, or that has a resolution incompatible with your own ideas of what looks good.

Re: Determining page height
by Your Mother (Archbishop) on Nov 13, 2005 at 07:08 UTC

    You can do this with JavaScript because items in the DOM know how high/wide they are rendered. But I'm absolutely with pg: too many ads is bad (more than one leaderboard? that's wack); coupled with it not being a Perl question, I won't post any solution code.