获取浏览器宽度和高度,不包括工具栏和菜单大小

Get Browser Width and Height Excluding Toolbars and Menu Size

本文关键字:工具栏 菜单 不包括 高度 浏览器 获取      更新时间:2023-09-26

我需要一个Js/JQuery脚本,它返回我浏览器的可用宽度和高度,不包括菜单栏和工具栏大小,我使用一个脚本,但它似乎返回宽度/高度,包括工具栏等等。。。

下面是我用过的脚本。。

<script type="text/javascript" >
var winWidth = 0, winHeight = 0;
        if (typeof (window.innerWidth) == 'number') {
            //Non-IE
            winWidth = window.innerWidth;
            winHeight = window.innerHeight;
        } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
            //IE 6+ in 'standards compliant mode'
            winWidth = document.documentElement.clientWidth;
            winHeight = document.documentElement.clientHeight;
        } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
            //IE 4 compatible
            winWidth = document.body.clientWidth;
            winHeight = document.body.clientHeight;
        }
</script>

有人知道这方面的线索吗?谢谢Meghana

使用jQuery,您可以获得以下内容:

$(window).height();   // returns height of browser viewport
$(document).height(); // returns height of HTML document

您的代码应该返回浏览器窗口的可用大小。在我的情况下,在1920x1200的显示器上,我得到1920x1106。我的任务栏有40像素高,所以剩下54像素作为窗口的标题栏。

try:

<input type='button' id='btn1' value='test'/>
$('#btn1').click(function(){
alert($(window).width());       
});

请参阅http://jsfiddle.net/Pu2Ej/