25

I'm trying to get the current browser viewport height, using

$(window).on('resize',function() { 
  console.log("new height is: "+$(window).height()); 
});

But I'm getting values that are way too low. When the viewport is around 850px tall, I'm getting values around 350 or 400px from height(). What's up?

Example: http://jsfiddle.net/forgetcolor/SVqx9/

mix
  • 6,943
  • 15
  • 61
  • 90

6 Answers6

58

I was having the same problem in Firefox then I added <!DOCTYPE HTML> on my index and it worked.

Source: http://viralpatel.net/blogs/jquery-window-height-incorrect/

Nilson Morais
  • 821
  • 1
  • 7
  • 6
12

One possible reason may be that you are checking the console with firebug/ something else. So that you don't get window height correct due to firebug height.

You can try something like this:

take a span/div in you document:

<span id="res"></span>

and then

$(window).on('resize',function() { 
  $('#res').html("new height is: "+$(window).height()); 
});

Or if you want to check out put on firebug console then detach it from browser and then check the result.

The System Restart
  • 2,873
  • 19
  • 28
5

For those who still has problem after above solutions...

Please check the view ratio of browser onto your project.html..

The value of $(window).height() is not as same as 'real' pixels of client area when the view ratio adjusted! (and so others $(xx).width()...in this situation)

(The browser remembered my careless 110% ratio adjusted days ago...)

ob.yann
  • 276
  • 3
  • 6
  • also look at ozzysong's reply [link](http://stackoverflow.com/a/23320757/233545) in another same problem – ob.yann Dec 24 '15 at 07:23
4

I have seen jQuery's $(window).height() returning a wrong value on Chrome mobile, even though my viewport and DOCTYPE declarations were in place. Eventually I used window.innerHeight.

jim_kastrin
  • 4,830
  • 2
  • 26
  • 28
3

No repro. Keep in mind that the window height is diminished by items in the browser's chrome, such as the address bar, developer tools, bookmarks toolbars and more. The following appears to show an accurate representation of the viewport height:

jsbin will give you a pretty good estimation of the window height, as it doesn't limit your code output to a small iframe like other js-testing sites such as jsfiddle.

http://jsbin.com/otaqej/edit#javascript,html

<!DOCTYPE html>
<html>
  <head>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.js"></script>
  </head>
  <body>
    <div id="message">Height: 0</div>
    <script>
      $(window).on("resize", function(){
        $("#message").html( "Height: " + $(window).height() );
      });
    </script>
  </body>
</html>
Sampson
  • 265,109
  • 74
  • 539
  • 565
  • Is there any reason behind jsbin not working in IE9? However it worked fine in Firefox. – Nazmul May 13 '12 at 04:53
  • @Hoque Remy Sharp, the creator of JSBin, has been testing it extensively in later versions of IE. If you have any issues with it that you can specifically detail, please let him know on twitter: [@rem](https://twitter.com/#!/rem). – Sampson May 13 '12 at 04:56
2

JSFiddle works by creating an <iframe> that gets loaded dynamically after you render the code.

Your JavaScript is calculating the height of the window, which happens to be the <iframe>'s height, which is around 400px tall for me.

Your code is doing what it should be.

Blender
  • 289,723
  • 53
  • 439
  • 496