JS實現獲取瀏覽器窗口尺寸

<!DOCTYPE html>
<html>
  <head>
        <title>請調整瀏覽器窗口</title>
        <meta charset="utf-8" />
        <meta http-equiv="content-type" content="text/html; charset=gb2312">
  </head>
<body>
 <h2 align="center">請調整瀏覽器窗口大小</h2><hr>
 <form action="#" method="get" name="form1" id="form1">
 <!--顯示瀏覽器窗口的實際尺寸-->
 瀏覽器窗口 的 實際高度: <input type="text" name="availHeight" size="4"><br>
 瀏覽器窗口 的 實際寬度: <input type="text" name="availWidth" size="4"><br>
 </form>
<script type="text/javascript">


    var winWidth = 0;
    var winHeight = 0;
    function findDimensions() //函數:獲取尺寸
    {
            //獲取窗口寬度
            if (window.innerWidth)
                winWidth = window.innerWidth;
            else if ((document.body) && (document.body.clientWidth))
                winWidth = document.body.clientWidth;
            //獲取窗口高度
            if (window.innerHeight)
                winHeight = window.innerHeight;
            else if ((document.body) && (document.body.clientHeight))
                winHeight = document.body.clientHeight;
            //經過深刻Document內部對body進行檢測,獲取窗口大小
            if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth)
            {
                winHeight = document.documentElement.clientHeight;
                winWidth = document.documentElement.clientWidth;
            }
            //結果輸出至兩個文本框
            document.form1.availHeight.value= winHeight;
            document.form1.availWidth.value= winWidth;
    }

    findDimensions();
    //調用函數,獲取數值
    window.onresize=findDimensions;

</script>

</body>
</html>

來源:http://blog.csdn.net/xxtjp/article/details/6948731javascript

相關文章
相關標籤/搜索