By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,187 Members | 1,060 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,187 IT Pros & Developers. It's quick & easy.

How to find height and width of webpage?

P: n/a
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

Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
brett wrote:
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.

--
Fred
Jul 23 '05 #2

P: n/a
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

Jul 23 '05 #3

P: n/a
brett wrote:
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.

--
Fred
Jul 23 '05 #4

P: n/a
Fred Oz wrote:
brett wrote:
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>

--
Fred

Jul 23 '05 #5

P: n/a
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

Jul 23 '05 #6

P: n/a
brett wrote:
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

--
Fred
Jul 23 '05 #7

P: n/a
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


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

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

I'm trying to create scalable image content that maintains its aspec
ratio, (like Flash does, which is not an option for this case.) I
there's a simpler way I'm not seeing, maybe someone could point me i
the right direction, but I've found surprisingly little of use on th
subject. Thanks in advance

--
Malkalyps
-----------------------------------------------------------------------
Malkalypse's Profile: http://www.highdots.com/forums/member.php?userid=2
View this thread: http://www.highdots.com/forums/showthread.php?t=7015

Jul 23 '05 #8

P: n/a
Malkalypse wrote:
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
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
Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.