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

安稳与你 提交于 2019-11-30 18:41:33

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.)

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 };
    }
}

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

$(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.

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..

Try the following code:

$(function(){
  $(window).on('cwsObjectsShown', function() {
    alert($(this).height());
  });   
});
Dénes

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!