使用Javascript检测观看区域的高度

时间:2009-06-09 10:39:41

标签: javascript dom browser height

我想使用Javascript检测可视区域的高度。我有这个高度为550px的DIV,我想在浏览器上显示。但是,此高度可能会导致垂直滚动条出现在某些浏览器上(取决于用户安装了多少个工具栏)。在这种情况下,我想检测到它,并提醒用户。

我尝试使用document.body.clientHeight,但它似乎无法正常工作...当我尝试添加新工具栏并刷新页面时,给我相同的高度。

3 个答案:

答案 0 :(得分:6)

答案 1 :(得分:4)

jQuery非常简单(并且在不同平台上运行良好):

<html>
    <head>
        <title>Heyo</title>
        <script type="text/javascript" src="jquery.js"></script>
    </head>
    <body>
        <script type="text/javascript">
            $(document).ready(function(){
                alert($(window).height());
                });
        </script>
    </body>
</html>

Documentation here

答案 2 :(得分:1)

YUI也很容易。

<html>
 <head>
  <title>Heya</title>
  <script type="text/javascript" src="http://yui.yahooapis.com/combo?3.0.0b1/build/yui/yui-min.js"></script>
 </head>
 <body>
   <script type="text/javascript">
    YUI().use('node', function(Y) {
      alert(Y.get(document).get('winHeight'));
    });
   </script>
 </body>
</html>

Documentation here

相关问题