1

I am curently using the follownig conditions, but they aren't working across browsers or at all:

if (typeof (window.innerHeight) == 'number') {
    //Non-IE:
    //Perform operation using window.innerWidth/Height
} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
    //IE 6+ in 'standards compliant mode'
    //Perform operation using document.documentElement.clientWidth/Height
} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
    //IE 4 compatible
    //Perform operation using document.body.clientWidth/Height
}

I believe I am missing a condition or two - perhaps there is another common expression I haven't taken into account.

What do I need to add to make this complete?

Giffyguy
  • 20,378
  • 34
  • 97
  • 168
  • 1
    Are you really still supporting IE4? The market share for IE<6 combined is somewhere around 1% isn't it? – josh.trow Nov 08 '10 at 18:31
  • @Josh.Trow: I pulled this off the internet somewhere - I'm just looking to support as many browsers as humanly possible. An extra condition for IE4 isn't really that much more complicated, eh? – Giffyguy Nov 08 '10 at 18:32
  • 1
    IE 4 is an ancient mummy. It was old even when I was younger. I can't think of anybody still trying to support it. You too really should not think about it. – Halil Özgür Nov 08 '10 at 18:36
  • Maybe not but I see no point to having extraneous code floating around, all it does is reduce readability. I work on projects with thousands of files with thousands of lines of code each, and clarity is absolutely king but I suppose it depends on the project. – josh.trow Nov 08 '10 at 18:38
  • Also, this q is kind of a dupe of http://stackoverflow.com/questions/833708/how-do-i-get-the-size-of-the-browser-window-using-prototype-js – josh.trow Nov 08 '10 at 18:39
  • @Josh.Trow: Oh, you're right, that code looks roughly identical. – Giffyguy Nov 08 '10 at 18:46

2 Answers2

1

Include jQuery in your pages and use $(window).width() and $(window).height().

Halil Özgür
  • 15,731
  • 6
  • 49
  • 56
  • 1
    Looking to do this in JavaScript, not JQuery. – Giffyguy Nov 08 '10 at 18:35
  • 5
    JQuery is Javascript, and opensource. If you don't want full jQuery support you can look into the source how they implemented those methods. – Mark Baijens Nov 08 '10 at 18:39
  • @Mark: That is an excelent point. Thanks. I may very well do that, as the JQuery methods are probably the best example on the market. – Giffyguy Nov 08 '10 at 18:48
  • 2
    I would argue that if someone asks for "javascript", they are not looking for a jquery solution, even if jquery runs in javascript and is open source. Yes they can look at the source, but why not just give them that information? – rob Nov 08 '10 at 18:49
  • 1
    @Giffyguy - Don't you dare believe that. Ever. – gblazex Nov 08 '10 at 18:50
  • 1
    @galambalazs, can you elaborate on why he wouldn't ever believe that? – Halil Özgür Nov 08 '10 at 18:54
  • 1
    Just because a lot of people use something, doesn't mean that it's always correct. In fact jQuery had/has many problems. It took Resig years to remove user agent sniffing from his code. He even wrote a book full of misconceptions http://bit.ly/cV1mVU. Also it's got one of the worst selector engines out there with a fundamentally broken abstraction http://bit.ly/c0ZliK, http://bit.ly/c3oiQK, http://bit.ly/99xQLS. All I'm saying is that jQuery isn't that shiny as one might naively think. It's surely **not** *`"the best example on the market"`*. – gblazex Nov 08 '10 at 20:23
  • 1
    Ok, can you suggest better alternative(s) with the functionality of jQuery? – Halil Özgür Nov 09 '10 at 13:51
0

You better off wrapping the whole logic into a function, so you only have to say getWindowHeight()

// cross browser window height
function getWindowHeight() {
  return window.innerHeight || 
         (document.documentElement && document.documentElement.clientHeight) || 
         (document.body && document.body.clientHeight);
}

Tested: IE5.5+, FF 2+, Chrome, Opera

But for an even more robust approach please visit the related comp.lang.javascript FAQ entry:
How do I find the size of the window?

gblazex
  • 49,155
  • 12
  • 98
  • 91