javascript - 使用javascript在宽屏上检测浏览器客户区大小

标签 javascript client area widescreen

多年来,我一直在使用以下代码检测浏览器客户区宽度,它在所有浏览器(包括 FF、Safari 和各种版本的 IE)中 100% 有效。但是,现在当我切换到宽屏分辨率 (1280x800) 的新显示器时,此代码在 IE8 上失败。它报告客户端宽度为 1024 !!!???

关于如何获得正确的客户区宽度有什么想法吗?

function getClientWidth() {
  var v=0,d=document,w=window;
  if((!d.compatMode || d.compatMode == 'CSS1Compat') && !w.opera && d.documentElement && d.documentElement.clientWidth)
    {v=d.documentElement.clientWidth;}
  else if(d.body && d.body.clientWidth)
    {v=d.body.clientWidth;}
  else if(xDef(w.innerWidth,w.innerHeight,d.height)) {
    v=w.innerWidth;
    if(d.height>w.innerHeight) v-=16;
  }
  return v;
}

最佳答案

我之前使用的非 jquery 代码:

function detectBrowserSize() {
    var myWidth = 0, myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth ||   document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    alert(myWidth + ' - ' + myHeight)
}

关于javascript - 使用javascript在宽屏上检测浏览器客户区大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1587499/

相关文章:

algorithm - 如何计算地球上自相交多边形的面积

javascript - 我需要将数据添加到 FabricJS 对象并在导出 Canvas 时使用它们

javascript - 如何从 Javascript 中的另一个私有(private)方法访问私有(private)方法

javascript - 如何使用 <select> <option> 切换 Twitter Bootstrap 选项卡 Pane ?

javascript - 如何使用强大的 npm 将文件上传到谷歌云

testing - 使用服务器测试不同编程语言的客户端的最佳方法是什么?

Java 客户端/服务器 SSL 套接字聊天

Python 套接字编程 - ConnectionRefusedError : [WinError 10061] No connection could be made because the target machine actively refused it

java - 如何在圆形范围内的android map 上生成标记

html - 将 css 应用于区域 map