首页 > 编程 > JS > 正文

JS获取各种浏览器窗口的大小

2020-09-19 11:13:01
字体:
来源:转载
供稿:网友

关于获取各种浏览器可见窗口大小的一点点研究

<script>
function getinfo()
{
    var s = "";
    s += " 网页可见区域宽:"+ document.body.clientwidth;
    s += " 网页可见区域高:"+ document.body.clientheight;
    s += " 网页可见区域宽:"+ document.body.offsetwidth + " (包括边线和滚动条的宽)";
    s += " 网页可见区域高:"+ document.body.offsetheight + " (包括边线的宽)";
    s += " 网页正文全文宽:"+ document.body.scrollwidth;
    s += " 网页正文全文高:"+ document.body.scrollheight;
    s += " 网页被卷去的高(ff):"+ document.body.scrolltop;
    s += " 网页被卷去的高(ie):"+ document.documentelement.scrolltop;
    s += " 网页被卷去的左:"+ document.body.scrollleft;
    s += " 网页正文部分上:"+ window.screentop;
    s += " 网页正文部分左:"+ window.screenleft;
    s += " 屏幕分辨率的高:"+ window.screen.height;
    s += " 屏幕分辨率的宽:"+ window.screen.width;
    s += " 屏幕可用工作区高度:"+ window.screen.availheight;
    s += " 屏幕可用工作区宽度:"+ window.screen.availwidth;
    s += " 你的屏幕设置是 "+ window.screen.colordepth +" 位彩色";
    s += " 你的屏幕设置 "+ window.screen.devicexdpi +" 像素/英寸";
    //alert (s);
}
getinfo();
</script>
在我本地测试当中:
在ie、firefox、opera下都可以使用
document.body.clientwidth
document.body.clientheight
即可获得,很简单,很方便。
而在公司项目当中:
opera仍然使用
document.body.clientwidth
document.body.clientheight
可是ie和firefox则使用
document.documentelement.clientwidth
document.documentelement.clientheight
原来是w3c的标准在作怪啊
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
如果在页面中添加这行标记的话

在ie中:
document.body.clientwidth ==> body对象宽度
document.body.clientheight ==> body对象高度
document.documentelement.clientwidth ==> 可见区域宽度
document.documentelement.clientheight ==> 可见区域高度
在firefox中:
document.body.clientwidth ==> body对象宽度
document.body.clientheight ==> body对象高度
document.documentelement.clientwidth ==> 可见区域宽度
document.documentelement.clientheight ==> 可见区域高度
?
在opera中:
document.body.clientwidth ==> 可见区域宽度
document.body.clientheight ==> 可见区域高度
document.documentelement.clientwidth ==> 页面对象宽度(即body对象宽度加上margin宽)
document.documentelement.clientheight ==> 页面对象高度(即body对象高度加上margin高)
而如果没有定义w3c的标准,则
ie为:
document.documentelement.clientwidth ==> 0
document.documentelement.clientheight ==> 0
firefox为:
document.documentelement.clientwidth ==> 页面对象宽度(即body对象宽度加上margin宽)document.documentelement.clientheight ==> 页面对象高度(即body对象高度加上margin高)
opera为:
document.documentelement.clientwidth ==> 页面对象宽度(即body对象宽度加上margin宽)document.documentelement.clientheight ==> 页面对象高度(即body对象高度加上margin高)
真是一件麻烦事情,其实就开发来看,宁可少一些对象和方法,不使用最新的标准要方便许多啊。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表