我想取得浏览器中,中间显示网页内容的区域大小(不包括滚动条),因为我想通过js建立一个div,使她能够正好覆盖浏览器的内容区域。
我在网上搜索到了这个:
function displayScreenSize()
        {
            var bodyWidth           =document.body.clientWidth;      //网页可见区域宽
            var bodyHeight          =document.body.clientHeight;     //网页可见区域高
            var bodyWidthWithBorder =document.body.offsetWidth;      //网页可见区域宽(包括边线的宽)
            var bodyHeightWithBorder=document.body.offsetHeight;     //网页可见区域高(包括边线的宽)
            var bodyWidthWithScroll =document.body.scrollWidth;      //网页正文全文宽
            var bodyHeightWithScroll=document.body.scrollHeight;     //网页正文全文高     
            var bodyTopHeight       =document.body.scrollTop;        //网页被卷去的上边距
            var bodyLeftWidth       =document.body.scrollLeft;       //网页被卷去的左边距
            var windowTopHeight     =window.screenTop;               //网页正文部分上边距
            var windowLeftWidth     =window.screenLeft;              //网页正文部分左边距 
            var screenHeight        =window.screen.height;           //屏幕分辨率的高
            var screenWidth         =window.screen.width;            //屏幕分辨率的宽
            var screenAvailHeight   =window.screen.availHeight;      //屏幕可用工作区高度 
            var screenAvailWidth    =window.screen.availWidth;       //屏幕可用工作区宽度
            
            var Str="<p>";
            Str+="网页可见区域宽:<span class='data'>"+bodyWidth+"px</span><br>";
            Str+="网页可见区域高:<span class='data'>"+bodyHeight+"px</span><br>";
            Str+="网页可见区域宽(包括边线的宽):<span class='data'>"+bodyWidthWithBorder+"px</span><br>";
            Str+="网页可见区域高(包括边线的宽):<span class='data'>"+bodyHeightWithBorder+"px</span><br>";
            Str+="网页正文全文宽:<span class='data'>"+bodyWidthWithScroll+"px</span><br>";
            Str+="网页正文全文高:<span class='data'>"+bodyHeightWithScroll+"px</span><br>";
            Str+="网页被卷去的上边距:<span class='data'>"+bodyTopHeight+"px</span><br>";
            Str+="网页被卷去的左边距:<span class='data'>"+bodyLeftWidth+"px</span><br>";
            Str+="网页正文部分上边距:<span class='data'>"+windowTopHeight+"px</span><br>";
            Str+="网页正文部分左边距:<span class='data'>"+windowLeftWidth+"px</span><br>";
            Str+="屏幕分辨率的高:<span class='data'>"+screenHeight+"px</span><br>";
            Str+="屏幕分辨率的宽:<span class='data'>"+screenWidth+"px</span><br>";
            Str+="屏幕可用工作区高度:<span class='data'>"+screenAvailHeight+"px</span><br>";
            Str+="屏幕可用工作区宽度:<span class='data'>"+screenAvailWidth+"px</span><br>";
            Str+="</p>"
            document.write(Str);
         }
但这最多只能给我的出宽,而不能给出合适的高。
请教大家有没有什么方法。

解决方案 »

  1.   

    终于最后还是我自己找到了一个//
    // getPageSize()
    // Returns array with page width, height and window width, height
    // Core code from - quirksmode.org
    // Edit for Firefox by pHaez
    //
    function getPageSize(){

    var xScroll, yScroll;

    if (window.innerHeight && window.scrollMaxY) {
    xScroll = document.body.scrollWidth;
    yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
    xScroll = document.body.scrollWidth;
    yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    xScroll = document.body.offsetWidth;
    yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;
    if (self.innerHeight) { // all except Explorer
    windowWidth = self.innerWidth;
    windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
    }

    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
    pageHeight = windowHeight;
    } else { 
    pageHeight = yScroll;
    } // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){
    pageWidth = windowWidth;
    } else {
    pageWidth = xScroll;
    }
    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
    return arrayPageSize;
    }