是否有一种方法适用于所有浏览器?


当前回答

只是为了将来参考:

function getscreenresolution()
{
    window.alert("Your screen resolution is: " + screen.height + 'x' + screen.width);
}

其他回答

您还可以获得窗口宽度和高度,避免浏览器工具栏和…(不仅仅是屏幕大小)。

要做到这一点,请使用: 窗口。innerWidth和window。innerHeight属性。详见w3schools。

在大多数情况下,这将是最好的方式,例如,显示一个完美的中心浮动模式对话框。它允许您计算窗口上的位置,无论使用浏览器的分辨率方向或窗口大小。

function getScreenWidth()
{
   var de = document.body.parentNode;
   var db = document.body;
   if(window.opera)return db.clientWidth;
   if (document.compatMode=='CSS1Compat') return de.clientWidth;
   else return db.clientWidth;
}

想要在移动设备上实现这个功能需要更多的步骤。屏幕上。不管设备的方向如何,availWidth保持不变。

以下是我的手机解决方案:

function getOrientation(){
    return Math.abs(window.orientation) - 90 == 0 ? "landscape" : "portrait";
};
function getMobileWidth(){
    return getOrientation() == "landscape" ? screen.availHeight : screen.availWidth;
};
function getMobileHeight(){
    return getOrientation() == "landscape" ? screen.availWidth : screen.availHeight;
};

使用jQuery,你可以做到:

$(window).width()
$(window).height()
var width = screen.width;
var height = screen.height;