JS获取浏览器宽和高

<form action="" name="form1">
    <input type="text" name="width"/>
    <input type="text" name="height"/>
</form>


<script type="text/javascript">
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.width.value= winWidth;
    document.form1.height.value= winHeight;
}
//调用函数,获取数值
findDimensions();

//关联窗口改变事件
window.onresize=findDimensions;
</script>
原文地址:https://www.cnblogs.com/dreamhome/p/2878556.html