Why does Firefox return 0 as the value of $(window).height()
Asked Answered
O

7

11

It seems to me that Firefox ought to be better equipped to return the proper value when using $(window).height().

I am using jQuery 1.8.2. Is this a bug in that particular build? Every other browser I have tested returns an appropriate value when calculating viewport height.

To work around this, I've used snip below to apply a pixel value to a div.

$(window).bind('cwsObjectsShown', function() {
  var height = $(window).height();
  if (height === 0) {
     height = window.innerHeight;
  }
  height = height - '120';
  $('#game_objects').css('height', height + 'px');
});
Onstad answered 17/10, 2012 at 15:10 Comment(2)
Are you calling this before there is any content on the page? $(window).height() works fine on firefox. Try calling it in the console to see what result you get.Illyrian
Using jQuery 1.8.2, I can get the window height even in the <head> of the document, before anything has loaded: jsfiddle.net/bdukes/HDrnxPancreatotomy
T
47

jQuery's $(window).height() is sensitive to doctype. Try <!doctype html> ?

The jQuery 1.8.1 release notes say

Don’t use Quirks mode! jQuery has never supported Quirks mode and we do not perform any testing in Quirks. This can affect values like $("window").height(), and the jQuery 1.8 Quirks mode results did change in order to support some modern browser features. The majority of problem cases we’ve seen are from developers that wanted to be in Standards mode but had an invalid doctype or extraneous markup before their tag. When in doubt, use the simple and short <!doctype html>.

For example, with jQuery 1.8.2 and Firefox 16, I get a valid $(window).height() with doctype html, but height 0 with doctype html5. In Chromium 20, both work. (The W3C HTML5 spec does say to use doctype html, not html5.)

Terrenceterrene answered 6/11, 2012 at 20:46 Comment(0)
R
4

Try this function:

function GetBrowserDim() {
    if (window.innerHeight) {
        return { w: window.innerWidth, h: window.innerHeight};
    } else {
        return { w: document.body.clientWidth, h: document.body.clientHeight };
    }
}
Raseda answered 3/9, 2016 at 11:22 Comment(0)
A
3

$(window).height() and also $(window).width() return 0 in IE when in compatibility mode. Not check this in FireFox, may be the same. try to use $(document).height() or $(document).width() instead.

Agitprop answered 14/4, 2016 at 9:52 Comment(0)
A
2

in my case i had to delete the attributes in the html element

<html lang="en-US" prefix="og: http://ogp.me/ns#">

changed to just

<html>

now i get the correct document width and height

Amplifier answered 22/1, 2015 at 5:23 Comment(0)
D
1

I found the answer to this strange render of height by jquery in Firefox...

I used class="no-js" in my html tag with doctype... when I removed this, FF calculated correctly... so I added this class with jquery like this $('html').addClass('no-js');

now the calulated is same in all browsers..

Disvalue answered 11/6, 2013 at 16:22 Comment(0)
S
1

You have to use <!DOCTYPE html> tag at the beginning, and everything will be fine.

Schaffhausen answered 21/4, 2019 at 13:20 Comment(0)
S
0

Try the following code:

$(function(){
  $(window).on('cwsObjectsShown', function() {
    alert($(this).height());
  });   
});
Spineless answered 17/10, 2012 at 15:17 Comment(1)
I've run it from console, using $(document).ready(), $(window).load(), and every time, the result is 0. I will try to create a test case for this at a url, though I'm beginning to suspect that this problem is very specific to other code occurring on the page.Onstad

© 2022 - 2024 — McMap. All rights reserved.