อ่านขนาดของหน้าเพจ

การอ่านความกว้างและความสูงของเพจทั้งหมด (พื้นที่แสดงผล) ด้วย Javascript (Cross Browser)

function getPageSizeWithScroll()
{
    if ( window.innerHeight && window.scrollMaxY ) // Firefox
    {
        yWithScroll = window.innerHeight + window.scrollMaxY;
        xWithScroll = window.innerWidth + window.scrollMaxX;
    }
    else if (document.body.scrollHeight > document.body.offsetHeight) // all but Explorer Mac
    {
        yWithScroll = document.body.scrollHeight;
        xWithScroll = document.body.scrollWidth;
    }
    else // works in Explorer 6 Strict, Mozilla (not FF) and Safari
    {
        yWithScroll = document.body.offsetHeight;
        xWithScroll = document.body.offsetWidth;
      }
    arrayPageSizeWithScroll = new Array( xWithScroll , yWithScroll );
    return arrayPageSizeWithScroll;
};

ตัวอย่าง

<script type="text/javascript">
var pagesize = getPageSizeWithScroll()
alert( 'ความกว้าง ' + pagesize[0] + ' ความสูง ' + pagesize[1] );
</script>


ผู้เขียน goragod โพสต์เมื่อ 22 เม.ย. 2551 เปิดดู 7,905 ป้ายกำกับ JavascriptDOM
^