views:

38

answers:

3

I am trying to determine the actual viewPORT size of the current browser window. I've tried:

  • window.innerHeight/innerWidth
  • document.documentElement.clientHeight/clientWidth
  • document.body.clientHeight/clientWidth

All return the full page size and NOT the viewing area.

What I'm trying to ultimately achieve is forcing a popup menu to appear on screen (in the viewport). Right now when it is shown, it might show below the scroll and the users are not happy with that. I know the x,y of where they've clicked. I just need to compare that to the size of the viewing area (with the size of the popup) to see if it will go offscreen.

It should be noted that the page is showing in an IFRAME, so if I need to go up one level to get the correct value, I can do that.

A: 

Try

document.getElementById("").offsetWidth

Fill the above code with different element ID's, try using the body tag, or a wrapper div.

Tom Gullen
Grrr... nope. Was hoping that was it. My monitor is 1900x1200 and offsetWidth with the Body tag is reporting: 1903x1308. 1903 is correct, but 1308 is the size of the page.
peiklk
+1  A: 
  • window.innerHeight/innerWidth

That unequivocally does give you viewport size (in this case, the size inside your iframe), but it isn't available on IE.

  • document.documentElement.clientHeight/clientWidth

That gives you viewport size, when the browser is in Standards mode. Typically used as fallback for IE.

  • document.body.clientHeight/clientWidth

That gives you viewport height in Quirks mode. You don't want to be in Quirks mode. Check the <!DOCTYPE of your document.

I just need to compare that to the size of the viewing area

Then you'll also have to look at the document.documentElement.scrollTop/scrollLeft.

bobince
window.innerHeight is giving the IFRAME size... it is not giving the viewport size :( This is in Firefox.
peiklk
If you are in an iframe, the iframe **is** your viewport. You can't possibly show any content outside of the bounds of the iframe. If the parent document is part of your site (same origin policy) you can call into it using `window.parent` and have *it* read its own viewport and add elements around/on-top-of your iframe.
bobince
Perhaps we're not communicating right on terminology.To me (and other sites I've researched this issue), the viewport is the area of the browser from the top menus to the bottom status bar. The actual area of the page that is currently in VIEW. So if the browser is full screen at 1900x1200 and the top IE/FF/Chrome menus take up say 200px and the status bar and Windows taskbar eat another 100 px, then the viewport height would be 900px (1200-200-100). The page could be anywhere from 0px to infinity pixels in height. If it's larger than the 900 available, you would see a scrollbar.
peiklk
A: 

Apparently by going to the parent document, I was able to get the correct value.

Thanks!

peiklk