How to find height and width of webpage?

B

brett

How can I find the height and width of a webpage? Say I want to make
sure someone's webpage is within an 800X600 viewing area. Width is the
most important but if I can get width, I should also be able to get
height.

I don't need to modify the page in anyway. Just get the width and
height. I can reference the page in an iframe, cfhttp (CFMX) or
something if it needs to be on my server.

Thanks,
Brett
 
F

Fred Oz

brett said:
How can I find the height and width of a webpage? Say I want to make
sure someone's webpage is within an 800X600 viewing area. Width is the
most important but if I can get width, I should also be able to get
height.

I don't need to modify the page in anyway. Just get the width and
height. I can reference the page in an iframe, cfhttp (CFMX) or
something if it needs to be on my server.

Have a read here, it's not as simple as it sounds.

<URL:http://www.quirksmode.org/js/>


First you need to figure out exactly what you mean by "the
height and width of a webpage" - it means different things in
different browsers.
 
B

brett

The number horizontal and vertical pixels. I'm not exactly sure how
that can be figured out. If their page can wrap, well, it has a
relative width and what does that mean? It should be fine. Maybe I
should just be looking for any tables or something with a hard coded
width greater than 800 right?

Brett
 
F

Fred Oz

brett said:
The number horizontal and vertical pixels. I'm not exactly sure how
that can be figured out. If their page can wrap, well, it has a
relative width and what does that mean? It should be fine. Maybe I
should just be looking for any tables or something with a hard coded
width greater than 800 right?

Brett

If you follow the link I provided, then click on the "viewport"
link and look for innerWidth/Height and clientWidth/Height and
read about document.body, you'll find out how to get it.

You must use different techniques for different browsers and the
results are also doctype dependencies to deal with.
 
F

Fred Oz

Fred said:
brett said:
The number horizontal and vertical pixels. I'm not exactly sure how
that can be figured out. If their page can wrap, well, it has a
[...]
You must use different techniques for different browsers and the results
are also doctype dependencies to deal with.

So here's some code, slightly modified from quirksmode. Note
that removing/changing the doctype changes IE's behaviour (a
different element is used and clicking below the bottom-most
element does not record a click in strict mode).


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>docment height/width</title>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">

<script type="text/javascript">
function doClick(){
var x, y, msg='';

// for all except Explorer
if (self.innerHeight) {
x = self.innerWidth;
y = self.innerHeight;
msg += 'self.innerHeight/Width: '
+ y + ', ' + x + 'px';

// Explorer 6 Strict Mode
} else if (document.documentElement
&& document.documentElement.clientHeight) {
x = document.documentElement.clientWidth;
y = document.documentElement.clientHeight;
msg += 'document.documentElement.clientHeight/Width: '
+ y + ', ' + x + 'px';

// other Explorers
} else if (document.body) {
x = document.body.clientWidth;
y = document.body.clientHeight;
msg += 'document.body.clientHeight/Width: '
+ y + ', ' + x + 'px';
}
alert(msg);
}
</script>
</head>
<body onclick="doClick();">
<div style="width: 200px; height: 200px;
border: 1px solid red;">
</div>
</body>
</html>
 
B

brett

That works nice. However, the above document should only be 200 wide.
It reports the browser doc width based on browser width, not the widest
tag element. I suppose I'm really looking for the widest "width"
attribute of any tag. That would give me the width based on what the
designer had in mind.

It can be tricky looking for width attributes. If a 100% width is
nested in a width=400, I might want to use your code to get the doc
width based on browser width. However, the true width is only 400.

Thanks,
Brett
 
F

Fred Oz

brett said:
That works nice. However, the above document should only be 200 wide.
It reports the browser doc width based on browser width, not the widest
tag element. I suppose I'm really looking for the widest "width"
attribute of any tag. That would give me the width based on what the
designer had in mind.

It can be tricky looking for width attributes. If a 100% width is
nested in a width=400, I might want to use your code to get the doc
width based on browser width. However, the true width is only 400.

The code only determines the internal width of the browser
window containing the document, it does not have any reference
to the width that the document requires or consumes.

I think you are getting too deep into HTML and CSS here.
JavaScript will help you find out whether elements have width
attributes defined, but that is only part of the story.

You must also consider width set by CSS - essentially, you have
to emulate the browser rendering engine. Can you deal with
relative & absolute positioned elements? Float left/right?
Display/visibility? Most browsers struggle to get it right.

If everyone used tables to layout their content, wouldn't life
be sooooo much simpler - friken' CSS-heads... :)

IIRC, most non-IE browsers will not report a width attribute if
none is specified, you have to use getComputedStyle - for those
browsers, getting the HTML established width is trivial (though
likely slow). However, IE seems to report attributes that
aren't set, as well as providing it's own version of a
getComputedStyle method too.

Finally, your job may be possible where the width is set
everywhere as pixels, but what about width as em, en or percent?
Do you know how to convert those to pixel widths?

Sorry to be such a pessimist! :-x
 
T

Thomas 'PointedEars' Lahn

Malkalypse said:
First off, I'm NOT being sarcastic here.. if the things you're talking
about really can be done with tables, I'd really like to know how?

Yes, you would.
And second, the question that led me here in the first place: whether
with tables or css, is there any way to dynamically convert percents
back to pixels? (i.e. as the user is rescaling their browser, it could
return the value in pixels of the height and width of things.)

Sure, you only have to re-implement a renderer as described in the CSS
specification. However, it would be much easier to use percents without
the need for client-side scripting in the first place.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top